当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Flink从入门到真香(3、从集合和文件中读取数据)
- 原创 | 数据资产确权浅议
- C language I blog assignment 03
- Python basic syntax variables
- Harbor项目高手问答及赠书活动
- YGC troubleshooting, let me rise again!
- 学习小结(关于深度学习、视觉和学习体会)
- The young generation of winner's programming life, the starting point of changing the world is hidden around
- 【Python 1-6】Python教程之——数字
- BCCOIN告诉您:年底最靠谱的投资项目是什么!
猜你喜欢
C language I blog assignment 03
Introduction to mongodb foundation of distributed document storage database
你的云服务器可以用来做什么?云服务器有什么用途?
运维人员常用到的 11 款服务器监控工具
C语言I博客作业03
Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
Written interview questions: find the smallest positive integer missing
PMP心得分享
【Python 1-6】Python教程之——数字
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
随机推荐
PMP考试通过心得分享
Q & A and book giving activities of harbor project experts
优化if-else代码的八种方案
Python basic syntax variables
python基础教程python opencv pytesseract 验证码识别的实现
The progress bar written in Python is so wonderful~
Ali tear off the e-commerce label
PMP experience sharing
ArrayList源码分析
Flink从入门到真香(10、Sink数据输出-Elasticsearch)
入门级!教你小程序开发不求人(附网盘链接)
Can you do it with only six characters?
如何将 PyTorch Lightning 模型部署到生产中
Win10 Terminal + WSL 2 安装配置指南,精致开发体验
PDMS cutting software
原创 | 数据资产确权浅议
Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Adobe media encoder / me 2021 software installation package (with installation tutorial)
Ubuntu20.04 access FTP server garbled problem + upload files