话不多说,先上效果图

屏幕截图方法,全屏截图按键盘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. Redis的java客户端jedis

    导包:Jedis需要的jar包 >Commons-pool-1.6.jar >Jedis-2.1.0.jar 配置:linux防火墙设置,不会设置就关闭. 停止防火墙 systemctl ...

  2. python 钉钉机器人发送消息

    import json import requests def sendmessage(message): url = 'https://oapi.dingtalk.com/robot/send?ac ...

  3. Python给照片换底色(蓝底换红底)

    环境要求 Python3 numpy函数库 opencv库 安装 下载适应版本的numpy函数库,我电脑是WIN10 64位,安装的函数库是 numpy-1.13.1+mkl-cp36-cp36m-w ...

  4. tornado web框架简介

    https://www.cnblogs.com/aylin/p/5702994.html

  5. CSAW 2016:Rock-100

    ELF文件 拖进ida 震惊: 这一堆堆都是什么神仙函数呀!! f5看一下伪代码 可以看出来 其实看起来函数很多 但是真正管用的函数也就那么几个 如果sub_4017E6的值是0 那么就会输出flag ...

  6. excel 公式 insert 语句

    ="insert into tb_fdn_deviceaccount (zdmc,czmc,sbbh,sbmc,SZCS,SBFLMC,SBLXMC,SBGG,SBYZ,SBJZ,SBXH, ...

  7. hibernate主配置文件中指定session与当前线程绑定

    配置一条属性 <property name="hibernate.current_session_context_class">thread</property& ...

  8. JS 单线程和事件循环

    Js 是单线程,js代码从上到下依次执行,比如我们写了两个函数,肯定是上面的函数先执行,下面的函数后执行.但是这种单线程有一个非常大的问题,那就是遇到耗时的任务,后面的任务只能等待它执行完,才能进行. ...

  9. 洛谷 P2151 [SDOI2009]HH去散步

    题目链接 思路 如果没有不能走上一条边的限制,很显然就是dp. 设f[i][j]表示到达i点走了j步的方案数,移到k点可以表示为f[k][j+1]+=f[i][j]. 如果有限制的话,可以考虑用边表示 ...

  10. MySQL数据库实现分页查询的SQL语句写法!

    一:分页需求: 客户端通过传递start(页码),limit(每页显示的条数)两个参数去分页查询数据库表中的数据,那我们知道MySql数据库提供了分页的函数limit m,n,但是该函数的用法和我们的 ...