当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Ubuntu20.04下访问FTP服务器乱码问题+上传文件
- Flink's sink: a preliminary study
- Bohai bank million level fines continue: Li Volta said that the governance is perfect, the growth rate is declining
- This time Kwai tiktok is faster than shaking.
- 分布式文档存储数据库之MongoDB基础入门
- Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
- 为 Docsify 自动生成 RSS 订阅
- What can your cloud server do? What is the purpose of cloud server?
- Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
- 渤海银行百万级罚单不断:李伏安却称治理完善,增速呈下滑趋势
猜你喜欢

Implementation of verification code recognition in Python opencv pytesseract

What can your cloud server do? What is the purpose of cloud server?

Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...

C language I blog assignment 03

供货紧张!苹果被曝 iPhone 12 电源芯片产能不足

PDMS cutting software

The young generation of winner's programming life, the starting point of changing the world is hidden around

C语言I博客作业03

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
随机推荐
第二次作业
Can you do it with only six characters?
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Bccoin tells you: what is the most reliable investment project at the end of the year!
入门级!教你小程序开发不求人(附网盘链接)
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
C language I blog assignment 03
We interviewed the product manager of SQL server of Alibaba cloud database, and he said that it is enough to understand these four problems
WLAN 直连(对等连接或 P2P)调研及iOS跨平台调研
Understanding design patterns
Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
一个方案提升Flutter内存利用率
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
TiDB 性能竞赛 11.02-11.06
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
On monotonous stack
On the confirmation of original data assets
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