话不多说,先上效果图

屏幕截图方法,全屏截图按键盘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实现多项选择的更多相关文章

  1. 解剖SQLSERVER 第十六篇 OrcaMDF RawDatabase --MDF文件的瑞士军刀(译)

    解剖SQLSERVER 第十六篇 OrcaMDF RawDatabase --MDF文件的瑞士军刀(译) http://improve.dk/orcamdf-rawdatabase-a-swiss-a ...

  2. Python之路【第十六篇】:Django【基础篇】

    Python之路[第十六篇]:Django[基础篇]   Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了O ...

  3. 跟我学SpringCloud | 第十六篇:微服务利剑之APM平台(二)Pinpoint

    目录 SpringCloud系列教程 | 第十六篇:微服务利剑之APM平台(二)Pinpoint 1. Pinpoint概述 2. Pinpoint主要特性 3. Pinpoint优势 4. Pinp ...

  4. Egret入门学习日记 --- 第十六篇(书中 6.10~7.3节 内容)

    第十六篇(书中 6.10~7.3节 内容) 昨天搞定了6.9节,今天就从6.10节开始. 其实这个蛮简单的. 这是程序员模式. 这是设计师模式. 至此,6.10节 完毕. 开始 6.11节. 有点没营 ...

  5. Python自动化 【第十六篇】:JavaScript作用域和Dom收尾

    本节内容: javascript作用域 DOM收尾 JavaScript作用域 JavaScript的作用域一直以来是前端开发中比较难以理解的知识点,对于JavaScript的作用域主要记住几句话,走 ...

  6. Struts2(十六篇)

    (一)Struts2框架概述 (二)Struts2配置文件 (三)Struts2的Action(简单讲解版) (四)Struts2的Action(深入讲解版) (五)Struts2处理结果管理 (六) ...

  7. 第十六篇 -- QListWidget与QToolButton(功能)

    效果图: 添加的部分,就是对几个action绑定了槽函数,完成相应的功能. listWidget操作的都是item,添加一个item,删除一个item,插入一个item等等.那么只需要知道item的几 ...

  8. Python之路(第三十六篇)并发编程:进程、同步异步、阻塞非阻塞

    一.理论基础 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所有内容都是围绕进程的概念展开的. 即使可以利用的cpu只有一个(早期的 ...

  9. 【Python之路】第十六篇--Web框架之Tornado

    概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...

随机推荐

  1. Linux基础学习笔记3-用户权限

    本章内容 用户user 令牌token,identity Linux用户:Uername/UID 管理员:root,0 普通用户:1-65535 系统用户:1-499,1-999(Centos7) 对 ...

  2. python之路--内置常用模块

    一 . 简单的了解模块 你写的每一个py文件都是一个模块. 还有一些我们一直在使用的模块. buildins  内置模块. print, input. random  主要是和随机相关的的内容 ran ...

  3. placeholder解决兼容各种IE浏览器的方法

    <input id="search" type="text" class="box" class="inputText&qu ...

  4. Java ME之Android开发从入门到精通

    1. 搭建Android开发环境 方式一:使用ADT插件安装 ADT插件的下载与安装,ADT插件获取网址:http://www.androiddevtools.cn/ 下载好的ADT插件如图所示: 在 ...

  5. FormDestroy 和 FormClose 有什么区别和联系?

    1.窗口的所有资源真正释放时调用 FormDestroy.当你关闭窗口时,VCL会调用FormClose,如果你在FormClose里写Action = caFree,那么VCL会继续调用FormDe ...

  6. Nginx 磁盘IO的优化

    L:132

  7. iptables防火墙的原理及应用

    简介 (netfilter, 位于Linux内核中的包过滤功能体系  ,称为Linux防火墙的“内核态”) iptables防火墙工作在网络层,针对TCP/IP数据包实施过滤和限制,iptables防 ...

  8. kubernetes 简单yaml文件运行例子deployment

    运行一个deployment: kubectl  run  nginx-deployment  --image=nginx:1.7.9  --replicas=2 基本例子: nginx-test.y ...

  9. 戴尔服务器H330阵列卡取消磁盘阵列教程

    一:服务器开机看到ctrl+R提示,按ctrl+r进入阵列卡配置界面 二:按ctrl+N 转到PD Mgmt查看硬盘信息,确认硬盘状态:Ready 三:光标移到需配置硬盘上,按F2,选择 conver ...

  10. robotframework测试用例加入注释

    *** Variables ***${HOST} 192.168.132.135${USER} username*** Test Cases ***Simple [Documentation] Sim ...