Android界面编程之利用单选框和复选框实现对学历和爱好进行选择
Android界面编程之利用单选框和复选框实现对学历和爱好进行选择
首先我们要了解一下单选框和复选框:
,
单选框(Radio Button):当用户选择某单选按钮时,同一组中的其他单选按钮不能同时选定,通常进行单选的按钮放到一个RadioGroup中
复选框(Check Box):可以选择任意数目,没有必要放到一个组中
定位被选中的按钮
单选框哪个按钮被选中时,需要在组中进行选择,复选框则不需要,所以单选框要对组进行监听,而复选框可以对个体进行监听,也可以创建一个数组用来循环扫描是否选中。
我使用数组来循环扫面,就不可以创建全局的变量了,不过写起来简单些,复制粘贴,出于代码的规范性最好选择不要创建数组,可以创建几个全局变量,然后自定义一个扫描的方法,本文就不一一掩饰了
下面看代码:
布局:采用Tablelayout布局
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent" android:layout_height="match_parent"><TableRow
android:id="@+id/tableRow1"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextView
android:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="学历"android:textSize="20sp"/><RadioGroup
android:id="@+id/radioGroup1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:orientation="horizontal"><RadioButton
android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:id="@+id/small"android:text="小学"/><RadioButton
android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/midle"android:text="中学"/><RadioButton
android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/large"android:text="大学"/><RadioButton
android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/secises"android:text="研究生"/></RadioGroup></TableRow><TableRow
android:id="@+id/tableRow2"android:layout_width="match_parent"android:layout_height="match_parent"><TextView
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="爱好:" /><LinearLayout
android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><CheckBox
android:id="@+id/run"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="跑步" /><CheckBox
android:id="@+id/swim"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="游泳" /><CheckBox
android:id="@+id/play"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="打球" /><CheckBox
android:id="@+id/read"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="读书" /></LinearLayout></TableRow><TextView
android:id="@+id/info"android:layout_width="wrap_content"android:layout_height="wrap_content" />
</TableLayout>
MainActivity
public class MainActivity extends AppCompatActivity {String stu;String hob;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tablelayout);final TextView info =(TextView)findViewById(R.id.info);RadioGroup radioGroup =(RadioGroup)findViewById(R.id.radioGroup1);final CheckBox checkBox[] = new CheckBox[4];checkBox[0] = (CheckBox)findViewById(R.id.swim);checkBox[1] = (CheckBox)findViewById(R.id.run);checkBox[2] = (CheckBox)findViewById(R.id.read);checkBox[3] = (CheckBox)findViewById(R.id.play);radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {if (i == R.id.small)stu="您的学历是小学";else if (i ==R.id.midle)stu="您的学历是中学";else if (i ==R.id.large)stu="您的学历是大学";else if (i==R.id.secises)stu="您的学历是研究生";info.setText(print());}});checkBox[0].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {hob = null;if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())hob="您的爱好是: ";for (int k = 0 ; k < checkBox.length ; k++ ){if (checkBox[k].isChecked())hob=hob+checkBox[k].getText().toString()+" ";}info.setText(print());}});checkBox[1].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {hob = null;if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())hob="您的爱好是: ";for (int k = 0 ; k < checkBox.length ; k++ ){if (checkBox[k].isChecked())hob=hob+checkBox[k].getText().toString()+" ";}info.setText(print());}});checkBox[2].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {hob = null;if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())hob="您的爱好是: ";for (int k = 0 ; k < checkBox.length ; k++ ){if (checkBox[k].isChecked())hob=hob+checkBox[k].getText().toString()+" ";}info.setText(print());}});checkBox[3].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {hob = null;if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked())hob="您的爱好是: ";for (int k = 0 ; k < checkBox.length ; k++ ){if (checkBox[k].isChecked())hob=hob+checkBox[k].getText().toString()+" ";}info.setText(print());}});}public String print(){String str = null;if(stu == null && hob == null){str = "请选择您的学历和爱好" ;}else if (stu != null && hob == null){str = stu ;}else if (stu == null && hob != null){str = hob ;}else {str = stu + "\n" + hob;}return str;}}
实现效果: 每次更改单选框或复选框都会更新提示
Android界面编程之利用单选框和复选框实现对学历和爱好进行选择相关推荐
- Android(Kotlin)图片按钮,单选框,复选框(实验课)
Android的一次"实验课布置的内容" 里面有一些可以擦考的东西 所以借着CSDN来记个笔记 其实也没啥 第一次写 留个纪念 图片按钮,单选框,复选框(话不多说上代码) butt ...
- android 单选按钮对号,用“对号”和“叉号”代替单选框和复选框选中或未选中代码详解...
用"对号"和"叉号"代替单选框和复选框选中或未选中代码,使用javascript+css可以实现这样的操作! 效果如图: /p> Transitional ...
- HTML 单选框和复选框的使用过程的一些错误
在开始学习radio的用法的时候,觉得很简单,但在用的时候,却不知道怎么进行单选,后来查了很长时间才明白其中的道理,其中,name的值是要相等的,这里的值是什么值都可以,只要相等就行 <inpu ...
- html中的下拉列表框,单选框和复选框
下拉列表框: 下拉列表框是指允许网页浏览者从下拉式菜单中选择某一项,我们通常会在网页中看到各种下拉列表框,这是一种最节省空间的方式.正常情况下,浏览者只能看见一个选项,单击选项按钮打开菜单后才能看到全 ...
- 纯css写单选框和复选框的样式和功能
只用纯css写的单选框和复选框的样式和功能该怎么写?看这里,复制下面的代码运行一遍就知道了,快试试吧! 效果截图: <!doctype html> <html lang=" ...
- css美化单选款、复选框
一款美化单选款.复选框的样式 支持度: Chrome Firefox Safari Opera IE9 && IE9+ 代码: <!DOCTYPE html> <ht ...
- html:(19):单选框,复选框,下拉列表框
使用单选框.复选框,让用户选择 在使用表单设计调查表时,为了减少用户的操作,使用选择框是一个好主意,html中有两种选择框,即单选框和复选框,两者的区别是单选框中的选项用户只能选择一项,而复选框中用户 ...
- 单选按钮带文字_一分钟教会你用Word添加单选框和复选框
又到学习Word技巧的时候啦!学了这么长时间,你的技能点有没有增加呢?对表格的使用有没有更加熟练了?是否很好奇别人家的表格是怎么弄成可以单选框和复选框的效果呢~~~~下面让小编带你解锁新姿势~ 效果图 ...
- java写html的多选框,Selenium+java - 单选框及复选框处理
Selenium+java - 单选框及复选框处理 一.什么是单选框.复选框? 二.被测页面html源代码 CheckBoxRadioDemo.html CheckBox.Radio练习案例 复选框 ...
最新文章
- 现在的学生太强了,徒手撸了一个小米商城项目(附源码)!
- 数组、结构体和共用体的长度计算?
- 利用Python3内置文档资源高效学习及官方中文文档
- 在linux中运行多文件,在Linux中打开了太多文件(Too many open files)的三种解决方法...
- 图像拐点检测-原理以及代码实现
- 杭电1280java实现
- CentOS6 安装 MySQL 并配置
- c语言迷宫算法设计,基于C语言的可连通迷宫算法设计及实现(毕业学术论文设计).doc...
- 为什么对gRPC做负载均衡会很棘手?
- 从命令式功能到纯功能性,然后再返回:Monads与范围内的延续
- python 描述器 详解_深入解析Python中的descriptor描述器的作用及用法
- 华为Android10版怎么截屏,安卓手机截图方法 华为手机如何截图 - 云骑士一键重装系统...
- 递推DP URAL 1119 Metro
- Phenotips 项目源码分析 [0]
- matplotlib,seaborn等画图工具
- 2022年最新《谷粒学院开发教程》:2 - 前后端交互篇
- 数论—乘法逆元—费马小定理
- 【愚公系列】2022年05月 vue3系列 axios请求的封装(TS版)
- 庄子心得06:总有路可走
- excel oss 上传_excel上传数据库失败
热门文章
- call(zoom)_如何解决您的Zoom Call问题
- css3从入门到熟练运用(三):炫目字体,多样背景和渐变颜色,神奇边框
- win10下cuda版本升级
- H3 BPM嵌入式流程解决方案 (文末附H3 BPM软件下载地址)
- pythonocc view coordinate_pythonOCC例子搬运:4.经典瓶子造型
- 二叉树的中序遍历,前序遍历,后序遍历
- 小白也能轻松上手selenium,无忧获取淘宝商品信息,献给手把手教的会的你
- 用户名不在sudoers文件中,此事将被报告
- 爬取bilibili视频
- PaddlePaddle深度学习实战——英法文翻译机