当前位置:网站首页>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
边栏推荐
- 应届生年薪35w+ !倒挂老员工,互联网大厂薪资为何越来越高?
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
- This time Kwai tiktok is faster than shaking.
- Iqkeyboardmanager source code to see
- 临近双11,恶补了两个月成功拿下大厂offer,跳槽到阿里巴巴
- Japan PSE certification
- How TCP protocol ensures reliable transmission
- The most complete! Alibaba economy cloud original practice! (Internet disk link attached)
猜你喜欢
个人目前技术栈
Istio traffic management -- progress gateway
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
浅谈单调栈
你的云服务器可以用来做什么?云服务器有什么用途?
Share the experience of passing the PMP examination
Ali tear off the e-commerce label
值得一看!EMR弹性低成本离线大数据分析最佳实践(附网盘链接)
Can you do it with only six characters?
Introduction to mongodb foundation of distributed document storage database
随机推荐
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
Get PMP certificate at 51CTO College
维图PDMS切图软件
笔试面试题目:求缺失的最小正整数
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
【计算机网络】学习笔记,第三篇:数据链路层(谢希仁版)
Understanding design patterns
年轻一代 winner 的程序人生,改变世界的起点藏在身边
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Learning summary (about deep learning, vision and learning experience)
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
[computer network] learning notes, Part 3: data link layer (Xie Xiren version)
Iqkeyboardmanager source code to see
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
Research on WLAN direct connection (peer-to-peer connection or P2P) and cross platform research of IOS
个人目前技术栈
笔试面试题目:求丢失的猪
Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
Improvement of rate limit for laravel8 update
Analysis of ArrayList source code