Android-----CheckBox复选使用(实现简单选餐)
直接上代码:
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复选使用(实现简单选餐)的更多相关文章
- 纯css3简单实用的checkbox复选框和radio单选框
昨天为大家分享了一款很炫的checkbox复选框和radio单选框,今天再给大家带来一款简单实用的checkbox复选框和radio单选框.界面清淅.舒服.先给大家来张效果图: 在线预览 源码下载 ...
- 03 CheckBox 复选框
五 CheckBox 复选框 >概念:可以从一个集合选项中选择一个或者多个选项 >属性:checked 选择状态 >使用: > ...
- checkbox复选框的一些深入研究与理解
一.一开始的唠叨最近忙于开发,自淫于项目的一步步完工,心浮躁了.舍近而求远,兵家之大忌.我是不是应该着眼于眼前的东西,好好的静下心来,超过一般人的沉静与沉浸,研究最基本的东西呢?这番思考,让我找到了一 ...
- 纯css3实现的超炫checkbox复选框和radio单选框
之前为大家分享了好多css3实现的按钮.今天要为大家分享的是纯css3实现的checkbox复选框和radio单选框,效果超级炫.先让我们看看图吧! 在线预览 源码下载 这个实例完全由css3实现 ...
- CheckBox复选框控件
CheckBox复选框控件 一.简介 1. 2.类结构图 二.CheckBox复选框控件使用方法 这里是使用java代码在LinearLayout里面添加控件 1.新建LinearLayout布局 2 ...
- 【转载】checkbox复选框的一些深入研究与理解
转载来自:原创文章,转载请注明来自张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com] 一.一开始的唠叨最近忙于开发,自淫于项目的一步步完工,心浮躁了.舍近而求远,兵家之大忌. ...
- css input checkbox复选框控件 样式美化的多种方案
checkbox复选框可能是网站中常用的html元素,但大多数人并不满意它的默认样式,这篇文章就讲讲如何实现input checkbox复选框控件 样式美化效果. 资源网站大全 https://55w ...
- CheckBox复选框全选以及获取值
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- php获取checkbox复选框的内容
php获取checkbox复选框的内容 由于checkbox属性,所有必须把checkbox复选择框的名字设置为一个如果checkbox[],php才能读取,以数据形式,否则不能正确的读取chec ...
- jquery checkbox 复选框多次点击判断选中状态,以及全选/取消的代码示例
2015年12月21日 10:52:51 星期一 目标, 点击当前的checbox, 判断点击后当前checkbox是否是选中状态. html: <input type="checkb ...
随机推荐
- ESA2GJK1DH1K升级篇: 远程升级准备工作: 使用TCP客户端连接Web服务器实现http下载数据
一,根目录建一个文件 二,使用浏览器访问 http://47.92.31.46:80/1.txt 或者 http://47.92.31.46/1.txt 三,使用TCP客户端访问文件内容 3 ...
- HBase快照迁移数据失败原因及解决办法
目录 目录 1 1. 背景 1 2. 环境 1 3. 执行语句 1 4. 问题描述 1 5. 错误信息 2 6. 问题原因 3 7. 解决办法 4 1. 背景 机房裁撤,需将源HBase集群的数据迁移 ...
- css如何让父元素下的所有子元素高度相同
小颖最近做的项目中要实现一个样式 ,小颖怕自己忘记了,写个随笔记下来 需求父元素下有多个子元素,并且子元素过多时要实现自动换行,给每个子元素都加了右边框,而每个子元素里的内容多少不一定,这就会产生右边 ...
- 爬虫之selenium模块chrome版本映射表
驱动及版本对应关系如下: 驱动下载路径见底部: chromedriver版本 支持的Chrome版本v2.43 v69-71v2.42 v68-70v2.41 v67-69v2.40 v66-68v2 ...
- C语言-malloc
malloc函数 编辑 锁定 同义词 malloc一般指malloc函数 malloc函数是一种分配长度为num_bytes字节的内存块的函数,可以向系统申请分配指定size个字节的内存空间.ma ...
- build gradle dependencies闭包的详解
转 :https://blog.csdn.net/guanguanboy/article/details/91043641 dependencies闭包的整体功能是指定当前项目所有依赖关系:本地依赖. ...
- element-ui复杂表单校验
复杂表单如下: <el-form size="mini" :rules="rules" :model="form" label-wid ...
- vue-router学习笔记(一)
学习vue-router首先要认识的两个属性 $router 和 $route. $router指的是router实例,$route则是当前激活的路由信息对象,是只读属性,不可更改,但是可以watch ...
- sqlserver替换一个单引号为多个单引号
SqlServer Where语句中如果有单引号,需要替换为两个单引号,不然会语法错误,替换方法如下REPLACE(@UserName,'''','''''') REPLACE(@UserName,' ...
- 【Pandas教程】像写SQL一样用Pandas~
写在最前 Python在数据分析领域有三个必须需要熟悉的库,分别是pandas,numpy和matplotlib,如果排个优先级的话,我推荐先学pandas. numpy主要用于数组和矩阵的运算,一般 ...