当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- On the confirmation of original data assets
- 笔试面试题目:判断单链表是否有环
- Flink从入门到真香(6、Flink实现UDF函数-实现更细粒度的控制流)
- 分布式文档存储数据库之MongoDB基础入门
- 浅谈单调栈
- Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
- How to deploy pytorch lightning model to production
- Introduction to mongodb foundation of distributed document storage database
- 虚拟机中安装 macOS 11 big sur
- Ali tear off the e-commerce label
猜你喜欢
Ali tear off the e-commerce label
适合c/c++新手学习的一些项目,别给我错过了!
C language I blog assignment 03
A scheme to improve the memory utilization of flutter
How to write a resume and project
211 postgraduate entrance examination failed, stay up for two months, get the byte offer! [face to face sharing]
Istio traffic management -- progress gateway
PMP心得分享
年轻一代 winner 的程序人生,改变世界的起点藏在身边
Share the experience of passing the PMP examination
随机推荐
ArrayList源码分析
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
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 basic syntax
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
解析Istio访问控制
Mozi college SQL injection solution
Entry level! Teach you how to develop small programs without asking for help (with internet disk link)
Written interview topic: looking for the lost pig
Harbor项目高手问答及赠书活动
Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
维图PDMS切图软件
笔试面试题目:求丢失的猪
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
为什么 Schnorr 签名被誉为比特币 Segwit 后的最大技术更新
Improvement of rate limit for laravel8 update
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
This year's salary is 35W +! Why is the salary of Internet companies getting higher and higher?
原创 | 数据资产确权浅议