当前位置:网站首页>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
边栏推荐
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
- 运维人员常用到的 11 款服务器监控工具
- [computer network] learning notes, Part 3: data link layer (Xie Xiren version)
- Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application
- 来自朋友最近阿里、腾讯、美团等P7级Python开发岗位面试题
- 为 Docsify 自动生成 RSS 订阅
- It's 20% faster than python. Are you excited?
- 分布式文档存储数据库之MongoDB基础入门
- Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
猜你喜欢
Written interview topic: looking for the lost pig
【计算机网络】学习笔记,第三篇:数据链路层(谢希仁版)
为什么 Schnorr 签名被誉为比特币 Segwit 后的最大技术更新
What can your cloud server do? What is the purpose of cloud server?
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
PMP心得分享
The container with the most water
2天,利用下班后的4小时开发一个测试工具
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
渤海银行百万级罚单不断:李伏安却称治理完善,增速呈下滑趋势
随机推荐
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
python基本语法 变量
Japan PSE certification
我们采访了阿里云云数据库SQL Server的产品经理,他说了解这四个问题就可以了...
维图PDMS切图软件
Python basic syntax variables
阿里撕下电商标签
Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
Can you do it with only six characters?
Improvement of rate limit for laravel8 update
PMP考试通过心得分享
Istio流量管理--Ingress Gateway
为 Docsify 自动生成 RSS 订阅
Harbor项目高手问答及赠书活动
Get PMP certificate at 51CTO College
Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
用科技赋能教育创新与重构 华为将教育信息化落到实处
运维人员常用到的 11 款服务器监控工具
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)