热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AndroidCheckBox实战

效果图:图1:图2:MainActivity.javapackageccv.turbosnail.xue_xi;importandro

效果图:

图1:

在这里插入图片描述

图2:

在这里插入图片描述
MainActivity.java

package ccv.turbosnail.xue_xi;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private CheckBox cbSeafood,cbFruits,cbCooler,cbMeat,cbVegetables; //多选按钮private TextView tvShow; //显示private Button btnQueDing; //确定private List<CheckBox> checkBoxList &#61; new ArrayList<>(); // 多选框集合&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView(); // 绑定id//追加到checkBoxList中checkBoxList.add(cbMeat);checkBoxList.add(cbFruits);checkBoxList.add(cbVegetables);checkBoxList.add(cbSeafood);checkBoxList.add(cbCooler);// 点击确定时btnQueDing.setOnClickListener(new View.OnClickListener() {&#64;Overridepublic void onClick(View v) {StringBuffer sb &#61; new StringBuffer(); //字符流跟字符串差不多&#xff0c;不过这样节省资源for (CheckBox checkbox : checkBoxList) { //遍历一遍集合if (checkbox.isChecked()) { //isChecked()是组件是否被选中&#xff0c;选中返回true,否则返回falsesb.append(checkbox.getText().toString() &#43; "\n\n"); //StringBuffer.append是字符串拼接}}tvShow.setText(sb);}});}private void initView() {cbCooler &#61; findViewById(R.id.cb_cooler);cbFruits &#61; findViewById(R.id.cb_fruits);cbMeat &#61; findViewById(R.id.cb_meat);cbSeafood &#61; findViewById(R.id.cb_seafood);cbVegetables &#61; findViewById(R.id.cb_vegetables);tvShow &#61; findViewById(R.id.tv_show);btnQueDing &#61; findViewById(R.id.btn_Queding);}}

activity_main.xml&#xff1a;

<?xml version&#61;"1.0" encoding&#61;"utf-8"?>
<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"xmlns:app&#61;"http://schemas.android.com/apk/res-auto"xmlns:tools&#61;"http://schemas.android.com/tools"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:orientation&#61;"vertical"tools:context&#61;".MainActivity"><TextViewandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:textSize&#61;"25dp"android:padding&#61;"20dp"android:text&#61;"你喜欢吃什么&#xff1f;"/><LinearLayoutandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"><CheckBoxandroid:id&#61;"&#64;&#43;id/cb_seafood"android:layout_marginTop&#61;"30dp"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:text&#61;"海鲜"/><CheckBoxandroid:id&#61;"&#64;&#43;id/cb_fruits"android:layout_marginTop&#61;"30dp"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_marginLeft&#61;"20dp"android:text&#61;"水果"/><CheckBoxandroid:id&#61;"&#64;&#43;id/cb_cooler"android:layout_marginTop&#61;"30dp"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_marginLeft&#61;"20dp"android:text&#61;"冷饮"/><CheckBoxandroid:id&#61;"&#64;&#43;id/cb_meat"android:layout_marginTop&#61;"30dp"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_marginLeft&#61;"20dp"android:text&#61;"肉"/><CheckBoxandroid:id&#61;"&#64;&#43;id/cb_vegetables"android:layout_marginTop&#61;"30dp"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_marginLeft&#61;"20dp"android:text&#61;"蔬菜"/></LinearLayout><Buttonandroid:id&#61;"&#64;&#43;id/btn_Queding"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:background&#61;"#0594fa"android:layout_marginTop&#61;"60dp"android:layout_gravity&#61;"center"android:textColor&#61;"#fff"android:textSize&#61;"18dp"android:text&#61;"确定"/><Viewandroid:layout_marginTop&#61;"20dp"android:layout_width&#61;"match_parent"android:layout_height&#61;"5dp"android:background&#61;"#fc0101"/><TextViewandroid:id&#61;"&#64;&#43;id/tv_show"android:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:layout_marginTop&#61;"20dp"android:textSize&#61;"20dp"/></LinearLayout>

CheckBox 属性&#xff1a;

在这里插入图片描述


推荐阅读
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文介绍了django中视图函数的使用方法,包括如何接收Web请求并返回Web响应,以及如何处理GET请求和POST请求。同时还介绍了urls.py和views.py文件的配置方式。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • 第一步:PyQt4Designer设计程序界面该部分设计类同VisvalStudio内的设计,改下各部件的objectName!设计 ... [详细]
  • [翻译]PyCairo指南裁剪和masking
    裁剪和masking在PyCairo指南的这个部分,我么将讨论裁剪和masking操作。裁剪裁剪就是将图形的绘制限定在一定的区域内。这样做有一些效率的因素࿰ ... [详细]
  • 在一对一直播源码使用过程中,有时会出现软键盘切换闪屏问题,就是当切换表情的时候屏幕会跳动,因此要对一对一直播源码表情面板无缝切换进行优化。 ... [详细]
  • 使用Flutternewintegration_test进行示例集成测试?回答首先在dev下的p ... [详细]
  • fileuploadJS@sectionscripts{<scriptsrc~Contentjsfileuploadvendorjquery.ui.widget.js ... [详细]
author-avatar
张晓和46872
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有