当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Where is the new target market? What is the anchored product? |Ten questions 2021 Chinese enterprise service
- It's 20% faster than python. Are you excited?
- Hematemesis! Alibaba Android Development Manual! (Internet disk link attached)
- Python基础语法
- Adobe media encoder / me 2021 software installation package (with installation tutorial)
- From a friend recently Ali, Tencent, meituan and other P7 Python development post interview questions
- IQKeyboardManager 源代码看看
- Share the experience of passing the PMP examination
- The container with the most water
- Python basic syntax variables
猜你喜欢

Adobe media encoder / me 2021 software installation package (with installation tutorial)

Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba

Python基础语法

Rust : 性能测试criterion库

11 server monitoring tools commonly used by operation and maintenance personnel

Tight supply! Apple's iPhone 12 power chip capacity exposed

Bccoin tells you: what is the most reliable investment project at the end of the year!

Adobe Lightroom /Lr 2021软件安装包(附安装教程)

How TCP protocol ensures reliable transmission

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
随机推荐
C语言I博客作业03
Python basic syntax
如何将 PyTorch Lightning 模型部署到生产中
Introduction to mongodb foundation of distributed document storage database
AQS analysis
当Kubernetes遇到机密计算,看阿里巴巴如何保护容器内数据的安全!(附网盘链接)
Understanding design patterns
漫画|讲解一下如何写简历&项目
华为云重大变革:Cloud&AI 升至华为第四大 BG ,火力全开
Share the experience of passing the PMP examination
Iqkeyboardmanager source code to see
Flink的sink实战之一:初探
Flink's sink: a preliminary study
C语言I博客作业03
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
2 days, using 4 hours after work to develop a test tool
This year's salary is 35W +! Why is the salary of Internet companies getting higher and higher?
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
吐血整理!阿里巴巴 Android 开发手册!(附网盘链接)
用科技赋能教育创新与重构 华为将教育信息化落到实处