当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Introduction to mongodb foundation of distributed document storage database
- Istio traffic management -- progress gateway
- 华为云重大变革:Cloud&AI 升至华为第四大 BG ,火力全开
- Written interview topic: looking for the lost pig
- AQS analysis
- How to deploy pytorch lightning model to production
- The container with the most water
- Mozi college SQL injection solution
- 你的云服务器可以用来做什么?云服务器有什么用途?
- Shell uses. Net objects to send mail
猜你喜欢
AQS analysis
Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Get PMP certificate at 51CTO College
Flink的sink实战之一:初探
漫画|讲解一下如何写简历&项目
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search
随机推荐
分布式文档存储数据库之MongoDB基础入门
第二次作业
维图PDMS切图软件
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
IQKeyboardManager 源代码看看
Share the experience of passing the PMP examination
为 Docsify 自动生成 RSS 订阅
Automatically generate RSS feeds for docsify
It's 20% faster than python. Are you excited?
The young generation of winner's programming life, the starting point of changing the world is hidden around
一文读懂机器学习“数据中毒”
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
你的云服务器可以用来做什么?云服务器有什么用途?
2 days, using 4 hours after work to develop a test tool
渤海银行百万级罚单不断:李伏安却称治理完善,增速呈下滑趋势
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
笔试面试题目:判断单链表是否有环
Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
A scheme to improve the memory utilization of flutter