直接上代码:

xml布局:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hs.example.exampleapplication.CheckBoxMain"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <CheckBox
android:id="@+id/chk1"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="汉堡"/> <CheckBox
android:id="@+id/chk2"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="薯条"/> <CheckBox
android:id="@+id/chk3"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="鸡翅"/> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <CheckBox
android:id="@+id/chk4"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="炸鸡"/> <CheckBox
android:id="@+id/chk5"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="蛋挞"/> <CheckBox
android:id="@+id/chk6"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="鸡块"/> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <CheckBox
android:id="@+id/chk7"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="可乐"/> <CheckBox
android:id="@+id/chk8"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="奶茶"/> <CheckBox
android:id="@+id/chk9"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="咖啡"/> </LinearLayout> <Button
android:id="@+id/btn_buy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="你的选餐如下:"/> <ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:id="@+id/food"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>

逻辑代码如下:

 public class CheckBoxMain extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{

     ArrayList<CompoundButton> selected = new ArrayList<>();//用来存储已选取项的集合对象

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox_main); /**【所有复选框id数组】**/
int chk_id [] = {R.id.chk1 ,R.id.chk2 ,R.id.chk3, R.id.chk4,
R.id.chk5 , R.id.chk6 , R.id.chk7 , R.id.chk8 , R.id.chk9}; /**【循环为所有复选框注册监听事件】**/
for(int id : chk_id){
CheckBox chk = findViewById(id);
chk.setOnCheckedChangeListener(this);
}
} @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){ //选项被选取
selected.add(compoundButton); //添加到集合中
}else { //选项被取消
selected.remove(compoundButton);//从集合中取消
} String msg = "";
for(CompoundButton chk : selected){
msg += chk.getText()+"\t\t\t"+" x 1 "+"\n";
} if(msg.length()== ){
msg = "请点餐!";
} TextView food = this.findViewById(R.id.food);
food.setTextSize();
food.setText(msg); } }

运行效果:

感兴趣的可以在这个基础上完善点餐的数量,单价,总价等功能。

Android-----CheckBox复选使用(实现简单选餐)的更多相关文章

  1. 纯css3简单实用的checkbox复选框和radio单选框

    昨天为大家分享了一款很炫的checkbox复选框和radio单选框,今天再给大家带来一款简单实用的checkbox复选框和radio单选框.界面清淅.舒服.先给大家来张效果图: 在线预览   源码下载 ...

  2. 03 CheckBox 复选框

    五  CheckBox  复选框     >概念:可以从一个集合选项中选择一个或者多个选项     >属性:checked   选择状态     >使用:           > ...

  3. checkbox复选框的一些深入研究与理解

    一.一开始的唠叨最近忙于开发,自淫于项目的一步步完工,心浮躁了.舍近而求远,兵家之大忌.我是不是应该着眼于眼前的东西,好好的静下心来,超过一般人的沉静与沉浸,研究最基本的东西呢?这番思考,让我找到了一 ...

  4. 纯css3实现的超炫checkbox复选框和radio单选框

    之前为大家分享了好多css3实现的按钮.今天要为大家分享的是纯css3实现的checkbox复选框和radio单选框,效果超级炫.先让我们看看图吧! 在线预览   源码下载 这个实例完全由css3实现 ...

  5. CheckBox复选框控件

    CheckBox复选框控件 一.简介 1. 2.类结构图 二.CheckBox复选框控件使用方法 这里是使用java代码在LinearLayout里面添加控件 1.新建LinearLayout布局 2 ...

  6. 【转载】checkbox复选框的一些深入研究与理解

    转载来自:原创文章,转载请注明来自张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com] 一.一开始的唠叨最近忙于开发,自淫于项目的一步步完工,心浮躁了.舍近而求远,兵家之大忌. ...

  7. css input checkbox复选框控件 样式美化的多种方案

    checkbox复选框可能是网站中常用的html元素,但大多数人并不满意它的默认样式,这篇文章就讲讲如何实现input checkbox复选框控件 样式美化效果. 资源网站大全 https://55w ...

  8. CheckBox复选框全选以及获取值

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  9. php获取checkbox复选框的内容

    php获取checkbox复选框的内容   由于checkbox属性,所有必须把checkbox复选择框的名字设置为一个如果checkbox[],php才能读取,以数据形式,否则不能正确的读取chec ...

  10. jquery checkbox 复选框多次点击判断选中状态,以及全选/取消的代码示例

    2015年12月21日 10:52:51 星期一 目标, 点击当前的checbox, 判断点击后当前checkbox是否是选中状态. html: <input type="checkb ...

随机推荐

  1. 最全BT磁力搜索引擎索引(整理分享,不断更新...)

    最全BT磁力搜索引擎索引(整理分享,不断更新...) btkitty:http://cnbtkitty.com/(知名的BT磁力搜索,资源很多) idope.se:https://idope.se/( ...

  2. ESA2GJK1DH1K基础篇: 移植官方MQTT包,让TCP实现MQTT功能(以GPRS模块为例)

    前言 这节代码将在这一节的基础上实现 拷贝第一节测试里面的MQTT文件夹到当前工程 当前工程建个MQTT的文件夹,用于存放那个MQTT文件夹里面的内容 添加文件到里面 注意:::: 实际源码拷贝位置 ...

  3. Java编程思想之十一 持有对象

    如果一个程序只包含固定数量的且其生命期都是已知的对象,那么这是一个非常简单的程序. 11.1 泛型和类型安全的容器 使用ArrayList:创建一个实例,用add()插入对象,然后用get()访问对象 ...

  4. 7种 JVM 垃圾收集器特点、优劣势及使用场景(多图)

    7种 JVM 垃圾收集器特点.优劣势及使用场景(多图)  mp.weixin.qq.com 点击上方"IT牧场",选择"设为星标"技术干货每日送达! 一.常见垃 ...

  5. Tomcat通过脚本自动部署

    1:autodeploy_tomcat_app.sh now=`date +%Y%m%d%H%M%S` tomcatPath=/home/test/apache-tomcat- backupPath= ...

  6. DB2 Error : SQLCODE=-802,SQLSTATE=22003 异常的原因

    使用DB2时,报错为 DB2 Error : SQLCODE=-802,SQLSTATE=22003 主要原因: 是我们sql语句使用某个字段*86400后结果非常大,导致无法返回结果:因为某个字段* ...

  7. mysql5.7报错ERROR 1819解决办法

    MySQL5.6.6版本之后增加了密码强度验证插件validate_password,相关参数设置的较为严格. 使用了该插件会检查设置的密码是否符合当前设置的强度规则,若不满足则拒绝设置.影响的语句和 ...

  8. 剑指阿里P6,25岁小伙怒斩三面,喜提offer(Java研发岗)

    本文提供者:洎扰の庸人 微信公众号:慕容千语的架构笔记.欢迎关注一起进步. 进阿里一直都是身为程序员的我,最初的梦想,经过去年面试蚂蚁金服失败的挫折后,今年再次鼓起勇气投简历,经过一位前辈的内推省了很 ...

  9. C++引用与常量

    常量: 在C++中有许多种数据类型(如int,float,bool等等).而这些数据类型又可以声明定义出变量与常量两种不同的具体数据.它们两种分类的标准是不一样的,是两个角度可以叠加的分类,举个栗子: ...

  10. C基本语法

    分号 ; 在C程序中,分好是语句结束符,每个语句必须以分好结束,它表明一个逻辑实体的结束 例如: printf("Hello, World! \n"); ; 注释 // 单行注释 ...