第十六篇-使用CheckBox实现多项选择
话不多说,先上效果图

屏幕截图方法,全屏截图按键盘print screen就行,活动窗口截图,按住ALT+print screen。
图片默认保存在home/picture路径下。可以通过自带的图片处理软件shotwell打开
先建一个empty Activity的项目,然后修改MainActivity.java,res/String.xml,activity_main.xml。
MainActivity.java
package com.example.aimee.checkboxtest; import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
CheckBox checkbox1;
CheckBox checkbox2;
CheckBox checkbox3;
CheckBox checkbox4;
Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkbox1=(CheckBox) findViewById(R.id.checkBox01);
checkbox2=(CheckBox) findViewById(R.id.checkBox02);
checkbox3=(CheckBox) findViewById(R.id.checkBox03);
checkbox4=(CheckBox) findViewById(R.id.checkBox04);
button=(Button) findViewById(R.id.Submit);
checkbox1.setOnCheckedChangeListener(new CheckBoxListener());
checkbox2.setOnCheckedChangeListener(new CheckBoxListener());
checkbox3.setOnCheckedChangeListener(new CheckBoxListener());
checkbox4.setOnCheckedChangeListener(new CheckBoxListener());
button.setOnClickListener((View.OnClickListener) new ButtonClickListener());
} class CheckBoxListener implements CompoundButton.OnCheckedChangeListener {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
if(isChecked){
Toast toast=Toast.makeText(MainActivity.this,buttonView.getText()+"被选择",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,5,5);
toast.show();
}else{
Toast toast=Toast.makeText(MainActivity.this,buttonView.getText()+"取消选择",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,5,5);
toast.show();
}
}
}
class ButtonClickListener implements View.OnClickListener{
public void onClick(View arg0){
String str="";
if(checkbox1.isChecked())
str=str+checkbox1.getText();
if(checkbox2.isChecked())
str=str+checkbox2.getText();
if(checkbox3.isChecked())
str=str+checkbox3.getText();
if(checkbox4.isChecked())
str=str+checkbox4.getText();
Toast.makeText(MainActivity.this,str+"被选择",Toast.LENGTH_LONG).show();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Title" /> <CheckBox
android:id="@+id/checkBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/Profile1"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="135dp" /> <CheckBox
android:id="@+id/checkBox02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/Profile2"
app:layout_constraintTop_toBottomOf="@+id/checkBox01"
tools:layout_editor_absoluteX="135dp"
tools:ignore="MissingConstraints" /> <CheckBox
android:id="@+id/checkBox03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/Profile3"
app:layout_constraintTop_toBottomOf="@+id/checkBox02"
tools:layout_editor_absoluteX="135dp"
tools:ignore="MissingConstraints" /> <CheckBox
android:id="@+id/checkBox04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/Profile4"
app:layout_constraintTop_toBottomOf="@+id/checkBox03"
tools:layout_editor_absoluteX="135dp"
tools:ignore="MissingConstraints" /> <Button
android:id="@+id/Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:text="@string/Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
String.xml
<resources>
<string name="app_name">CheckboxTest</string>
<string name="Title">请选择喜欢的情景模式</string>
<string name="Profile1">上班模式</string>
<string name="Profile2">家庭模式</string>
<string name="Profile3">旅游模式</string>
<string name="Profile4">会议模式</string>
<string name="Submit">Submit</string>
</resources>
至此,一个简单的多项选择就完成了。
有兴趣也可以设置其他的项目。
第十六篇-使用CheckBox实现多项选择的更多相关文章
- 解剖SQLSERVER 第十六篇 OrcaMDF RawDatabase --MDF文件的瑞士军刀(译)
解剖SQLSERVER 第十六篇 OrcaMDF RawDatabase --MDF文件的瑞士军刀(译) http://improve.dk/orcamdf-rawdatabase-a-swiss-a ...
- Python之路【第十六篇】:Django【基础篇】
Python之路[第十六篇]:Django[基础篇] Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了O ...
- 跟我学SpringCloud | 第十六篇:微服务利剑之APM平台(二)Pinpoint
目录 SpringCloud系列教程 | 第十六篇:微服务利剑之APM平台(二)Pinpoint 1. Pinpoint概述 2. Pinpoint主要特性 3. Pinpoint优势 4. Pinp ...
- Egret入门学习日记 --- 第十六篇(书中 6.10~7.3节 内容)
第十六篇(书中 6.10~7.3节 内容) 昨天搞定了6.9节,今天就从6.10节开始. 其实这个蛮简单的. 这是程序员模式. 这是设计师模式. 至此,6.10节 完毕. 开始 6.11节. 有点没营 ...
- Python自动化 【第十六篇】:JavaScript作用域和Dom收尾
本节内容: javascript作用域 DOM收尾 JavaScript作用域 JavaScript的作用域一直以来是前端开发中比较难以理解的知识点,对于JavaScript的作用域主要记住几句话,走 ...
- Struts2(十六篇)
(一)Struts2框架概述 (二)Struts2配置文件 (三)Struts2的Action(简单讲解版) (四)Struts2的Action(深入讲解版) (五)Struts2处理结果管理 (六) ...
- 第十六篇 -- QListWidget与QToolButton(功能)
效果图: 添加的部分,就是对几个action绑定了槽函数,完成相应的功能. listWidget操作的都是item,添加一个item,删除一个item,插入一个item等等.那么只需要知道item的几 ...
- Python之路(第三十六篇)并发编程:进程、同步异步、阻塞非阻塞
一.理论基础 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所有内容都是围绕进程的概念展开的. 即使可以利用的cpu只有一个(早期的 ...
- 【Python之路】第十六篇--Web框架之Tornado
概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...
随机推荐
- PHP5.4.0新特性研究
PHP5.4.0新特性研究 1.内建Web Server 这的确是个好的改进,大大的方便了开发人员.以后开发机不装nginx,httpd也行 cd $PHP_INSTALL_PATH ./bin/ph ...
- Ubuntu中MySql的启动与关闭
安装mysql sudo apt-get install mysql-server sudo apt install mysql-client sudo apt install libmysqlcli ...
- VMware与CentOS的安装与Linux简单指令
一 . VMware与CentOS系统安装 下载CentOS系统的ISO镜像 # 官方网站,国外网站,下载速度会很慢 www.centos.org # 由于国外的下载速度慢,我们可以使用国内的镜像源 ...
- drf开发中常见问题
开发常见问题及解决 问题: 一.本地系统不能重现的bug 二.api接口出错不能及时的发现或难找到错误栈 三.api文档管理问题 四.大量的url配置造成url配置越来越多难以维护 五.接口不及时去更 ...
- awk骚操作
一.awk自加 [root@168web3 ~]# head /data/logs/cloud_monitor_rds_cpu.log |awk '{sum+=$NF}END{print sum}' ...
- Jsoup的使用
http://caidongrong.blog.163.com/blog/static/21424025220139292525874/
- Logging - MVC Using Log4net Save to File and Database
第一步:创建Config文件夹和log4net.config 第二步:在log4net.confg黏贴以下配置 <?xml version="1.0" encoding=&q ...
- BZOJ3224普通平衡树——旋转treap
题目: 此为平衡树系列第一道:普通平衡树您需要写一种数据结构,来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数, ...
- python中的logging模块学习
Python的logging模块 Logging的基本信息: l 默认的情况下python的logging模块打印到控制台,只显示大于等于warning级别的日志 l 日志级别:critical ...
- Java将Excel中科学计数法解析成数字
需要注意的是一般的科学表达式是1.8E12 1.8E-12 而在Excel中的科学表达式是1.8E+12 1.8E-12 我写的科学计数法的正则表达式是(-?\d+\.?\d*)[Ee]{1}[\+- ...