当前位置:网站首页>Android Basics - check box
Android Basics - check box
2020-11-08 12:06:00 【ZHAO_JH】
CheckBox( Check box )
You can select multiple options at the same time , As for getting the selected value , There are also two ways : 1. For each CheckBox Add event :setOnCheckedChangeListener 2. Get a button , After clicking , For each checkbox Judge :isChecked();
Example layout activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<CheckBox
android:id="@+id/cbSleep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" sleep " />
<CheckBox
android:id="@+id/cbSing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Sing a song " />
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Output "
android:onClick="show" />
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnCheckedChangeListener {
// establish map Dictionaries are used to store selected content
private Map map_dict = new HashMap();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get controls
CheckBox cbSing = (CheckBox) findViewById(R.id.cbSing);
CheckBox cbSleep = (CheckBox) findViewById(R.id.cbSleep);
// The binding event
cbSing.setOnCheckedChangeListener(this);
cbSleep.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton checkBox, boolean checked) {
// Judge whether to select , Select to add data to map In the dictionary , If there is none, it will be removed by default mao All the data in the dictionary
switch (checkBox.getId()) {
case R.id.cbSing:
if (checked) {
map_dict.put("sing", " Sing a song ");
}else {
map_dict.remove("sing");
}
break;
case R.id.cbSleep:
if (checked) {
map_dict.put("sleep", " sleep ");
}else {
map_dict.remove("sleep");
}
break;
default:
break;
}
}
public void show(View v) {
// Judge map Whether there is data in the dictionary
if(map_dict.isEmpty()){
Toast.makeText(MainActivity.this, " No data selected ", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), " Your chosen hobby is :" + map_dict, Toast.LENGTH_SHORT).show();
}
}
}
版权声明
本文为[ZHAO_JH]所创,转载请带上原文链接,感谢
边栏推荐
- Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
- 用 Python 写出来的进度条,竟如此美妙~
- Automatically generate RSS feeds for docsify
- 一文读懂机器学习“数据中毒”
- We interviewed the product manager of SQL server of Alibaba cloud database, and he said that it is enough to understand these four problems
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- 【Python 1-6】Python教程之——数字
- What can your cloud server do? What is the purpose of cloud server?
- Python基础语法
- Shell uses. Net objects to send mail
猜你喜欢
【Python 1-6】Python教程之——数字
PMP心得分享
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
学习小结(关于深度学习、视觉和学习体会)
一文读懂机器学习“数据中毒”
The young generation of winner's programming life, the starting point of changing the world is hidden around
Tidb performance competition 11.02-11.06
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application
BCCOIN告诉您:年底最靠谱的投资项目是什么!
随机推荐
Bohai bank million level fines continue: Li Volta said that the governance is perfect, the growth rate is declining
分布式文档存储数据库之MongoDB基础入门
android基础-CheckBox(复选框)
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
It's 20% faster than python. Are you excited?
漫画|讲解一下如何写简历&项目
Understanding design patterns
next.js实现服务端缓存
笔试面试题目:判断单链表是否有环
TiDB 性能竞赛 11.02-11.06
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
适合c/c++新手学习的一些项目,别给我错过了!
为什么 Schnorr 签名被誉为比特币 Segwit 后的最大技术更新
入门级!教你小程序开发不求人(附网盘链接)
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
Can you do it with only six characters?
C语言I博客作业03
How to deploy pytorch lightning model to production
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...