当前位置:网站首页>android基础-CheckBox(复选框)
android基础-CheckBox(复选框)
2020-11-08 12:06:00 【ZHAO_JH】
CheckBox(复选框)
即可以同时选中多个选项,至于获得选中的值,同样有两种方式: 1.为每个CheckBox添加事件:setOnCheckedChangeListener 2.弄一个按钮,在点击后,对每个checkbox进行判断:isChecked();
布局示例 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="睡觉" />
<CheckBox
android:id="@+id/cbSing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="唱歌" />
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输出"
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 {
// 创建map字典用于储存选择的内容
private Map map_dict = new HashMap();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取控件
CheckBox cbSing = (CheckBox) findViewById(R.id.cbSing);
CheckBox cbSleep = (CheckBox) findViewById(R.id.cbSleep);
// 绑定事件
cbSing.setOnCheckedChangeListener(this);
cbSleep.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton checkBox, boolean checked) {
// 判断是否选中,选中就把数据加入到map字典中,如果都没有的话默认移除mao字典所有数据
switch (checkBox.getId()) {
case R.id.cbSing:
if (checked) {
map_dict.put("sing", "唱歌");
}else {
map_dict.remove("sing");
}
break;
case R.id.cbSleep:
if (checked) {
map_dict.put("sleep", "睡觉");
}else {
map_dict.remove("sleep");
}
break;
default:
break;
}
}
public void show(View v) {
// 判断map字典中是否存在数据
if(map_dict.isEmpty()){
Toast.makeText(MainActivity.this, "没有选中任何数据", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "您选中的爱好是:" + map_dict, Toast.LENGTH_SHORT).show();
}
}
}
版权声明
本文为[ZHAO_JH]所创,转载请带上原文链接,感谢
https://my.oschina.net/zhaojunhui/blog/4708090
边栏推荐
- How to deploy pytorch lightning model to production
- 211考研失败后,熬夜了两个月拿下字节offer!【面经分享】
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- 墨者学院SQL注入解题
- laravel8更新之速率限制改进
- On monotonous stack
- Analysis of ArrayList source code
- 在51CTO学院Get到PMP证书
- 临近双11,恶补了两个月成功拿下大厂offer,跳槽到阿里巴巴
- Ali! Visual computing developer's series of manuals (with internet disk link)
猜你喜欢

阿里撕下电商标签

Written interview questions: find the smallest positive integer missing

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

墨者学院SQL注入解题

Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)

不多不少,大学里必做的五件事(从我的大一说起)

运维人员常用到的 11 款服务器监控工具

用科技赋能教育创新与重构 华为将教育信息化落到实处

Ali tear off the e-commerce label

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
随机推荐
Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
How TCP protocol ensures reliable transmission
We interviewed the product manager of SQL server of Alibaba cloud database, and he said that it is enough to understand these four problems
Share the experience of passing the PMP examination
Research on WLAN direct connection (peer-to-peer connection or P2P) and cross platform research of IOS
墨者学院SQL注入解题
[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter
laravel8更新之速率限制改进
PDMS cutting software
Win10 Terminal + WSL 2 安装配置指南,精致开发体验
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Written interview topic: looking for the lost pig
Rust: performance test criteria Library
Entry level! Teach you how to develop small programs without asking for help (with internet disk link)
如何将 PyTorch Lightning 模型部署到生产中
This time Kwai tiktok is faster than shaking.
Rust: command line parameter and environment variable operation
On monotonous stack
Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
It's 20% faster than python. Are you excited?