当前位置:网站首页>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
边栏推荐
- Hematemesis! Alibaba Android Development Manual! (Internet disk link attached)
- Automatically generate RSS feeds for docsify
- 你的云服务器可以用来做什么?云服务器有什么用途?
- Get PMP certificate at 51CTO College
- Harbor项目高手问答及赠书活动
- Flink的sink实战之一:初探
- ArrayList源码分析
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- 值得一看!EMR弹性低成本离线大数据分析最佳实践(附网盘链接)
- 运维人员常用到的 11 款服务器监控工具
猜你喜欢

阿里撕下电商标签

Analysis of ArrayList source code

Harbor项目高手问答及赠书活动

Rust : 性能测试criterion库

If you don't understand the gap with others, you will never become an architect! What's the difference between a monthly salary of 15K and a monthly salary of 65K?

你搞不懂与别人的差距,永远成不了架构师!月薪15K和月薪65K,你差在那了?

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

Flink的sink实战之一:初探

Written interview topic: looking for the lost pig

Istio流量管理--Ingress Gateway
随机推荐
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Where is the new target market? What is the anchored product? |Ten questions 2021 Chinese enterprise service
在51CTO学院Get到PMP证书
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
比Python快20%,就问你兴不兴奋?
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
How TCP protocol ensures reliable transmission
一文剖析2020年最火十大物联网应用|IoT Analytics 年度重磅报告出炉!
Python基础语法
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Q & A and book giving activities of harbor project experts
VC++指定目录下文件按时间排序输出
个人目前技术栈
Python basic syntax variables
应届生年薪35w+ !倒挂老员工,互联网大厂薪资为何越来越高?
用 Python 写出来的进度条,竟如此美妙~
Mozi college SQL injection solution
Powershell 使用.Net对象发送邮件
渤海银行百万级罚单不断:李伏安却称治理完善,增速呈下滑趋势