当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- android基础-CheckBox(复选框)
- YGC troubleshooting, let me rise again!
- 为什么 Schnorr 签名被誉为比特币 Segwit 后的最大技术更新
- 浅谈单调栈
- Where is the new target market? What is the anchored product? |Ten questions 2021 Chinese enterprise service
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- 用科技赋能教育创新与重构 华为将教育信息化落到实处
- Adobe Lightroom /Lr 2021软件安装包(附安装教程)
- 如何将 PyTorch Lightning 模型部署到生产中
- Learning summary (about deep learning, vision and learning experience)
猜你喜欢
Mozi college SQL injection solution
华为云重大变革:Cloud&AI 升至华为第四大 BG ,火力全开
Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
Rust: performance test criteria Library
在51CTO学院Get到PMP证书
YGC troubleshooting, let me rise again!
Analysis of ArrayList source code
Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
年轻一代 winner 的程序人生,改变世界的起点藏在身边
Why is Schnorr Signature known as the biggest technology update after bitcoin segwit
随机推荐
TCP协议如何确保可靠传输
Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
Implementation of verification code recognition in Python opencv pytesseract
笔试面试题目:求缺失的最小正整数
解析Istio访问控制
211考研失败后,熬夜了两个月拿下字节offer!【面经分享】
Flink从入门到真香(10、Sink数据输出-Elasticsearch)
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
笔试面试题目:求丢失的猪
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
不多不少,大学里必做的五件事(从我的大一说起)
原创 | 数据资产确权浅议
On monotonous stack
华为云重大变革:Cloud&AI 升至华为第四大 BG ,火力全开
android基础-CheckBox(复选框)
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
漫画|讲解一下如何写简历&项目
Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
浅谈单调栈
It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)