radiogroup多选_Android实现单项、多项选择操作
本文实例为大家分享了Android实现单项、多项选择操作的相关代码,供大家参考,具体内容如下
1、单项选择
1.1.布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.rj141.sb.kongjian.DateActivity">
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2+3="
android:textSize="22dp"
/>
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A.2"
android:id="@+id/rb1"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B.3"
android:id="@+id/rb2"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C.4"
android:id="@+id/rb3"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D.5"
android:id="@+id/rb4"
/>
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"/>
1.2.Java文件
public class SingChoose extends AppCompatActivity {
private Button btn;
private RadioButton rbD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sing_choose);
rbD= (RadioButton) this.findViewById(R.id.rb4);
btn= (Button) this.findViewById(R.id.submit);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(rbD.isChecked()){
Toast.makeText(SingChoose.this,"正确,请加五分",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(SingChoose.this,"错误,请减五分",Toast.LENGTH_SHORT).show();
}
}
});
}
}
效果图:
2、多项选择
2.1.布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.rj141.sb.kongjian.CheckChoose">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="你喜欢下列哪些物品?"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="巧克力"
android:id="@+id/cb1" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="冰淇淋"
android:id="@+id/cb2" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蛋糕"
android:id="@+id/cb3" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啤酒"
android:id="@+id/cb4" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="肉"
android:id="@+id/cb5" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:id="@+id/tv" />
2.2.Java文件
public class CheckChoose extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private CheckBox cb1,cb2,cb3,cb4,cb5;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_choose);
tv= (TextView) this.findViewById(R.id.tv);
cb1= (CheckBox) this.findViewById(R.id.cb1);
cb2= (CheckBox) this.findViewById(R.id.cb2);
cb3= (CheckBox) this.findViewById(R.id.cb3);
cb4= (CheckBox) this.findViewById(R.id.cb4);
cb5= (CheckBox) this.findViewById(R.id.cb5);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
cb4.setOnCheckedChangeListener(this);
cb5.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String str="您喜欢:";
if(cb1.isChecked()){
str+=cb1.getText()+",";
}
if(cb2.isChecked()){
str+=cb2.getText()+",";
}
if(cb3.isChecked()){
str+=cb3.getText()+",";
}
if(cb4.isChecked()){
str+=cb4.getText()+",";
}
if(cb5.isChecked()){
str+=cb5.getText()+",";
}
tv.setText(str);
}
}
效果图:
以上就是本文的全部内容,希望对大家学习Android软件编程有所帮助。
radiogroup多选_Android实现单项、多项选择操作相关推荐
- radiogroup多选_Android 自定义View实现多行RadioGroup (MultiLineRadioGroup)
一.项目概况 我们都知道RadioGroup可以实现选择框,但它有一个局限性,由于它是继承自LinearLayout的,所以只能有一个方向,横向或者纵向:但有时候仅一行的RadioGroup并不能满足 ...
- Android学习笔记17:单项选择RadioButton和多项选择CheckBox的使用
在Android中,可以通过RadioButton和RadioGroup的组合来实现单项选择的效果.而多项选择则是通过CheckBox来实现的. 1.单项选择RadioButton 我们知道,一个单项 ...
- 创建输入控件(input控件、文本框、密码框、单项选择、多项选择、重置与提交按钮的设置)
创建输入控件 input控件的相关概念 input控件的属性 input控件的类型 文本框的设置 密码框的设置 单项选择的设置 多项选择的设置 重置与提交按钮的设置 综合运用 相关概念选择题及参考答案 ...
- radiogroup多选_类似 RadioGroup 的选中管理布局
按照惯例,先上图: Screenshot_2018-09-04-19-42-51-171_中国移动.png 类似这样的n选一的需求是很常见的,用原生的 RadioGroup 几乎无法实现,那么就需要一 ...
- QT之CheckBox单项选择与多项选择
QT之CheckBox单项选择与多项选择 QT之CheckBox单项选择与多项选择 开篇之叙 效果图 上代码 结尾 开篇之叙 今天简单的给大家介绍下CheckBox的单项选择与多项选择.我们在使用Ch ...
- uview Checkbox 复选框勾选,删除某一项遇见的bug,复用导致的问题
uview Checkbox 复选框勾选,删除某一项遇见的bug 如下图,所示,正常新建了子任务123,1跟3勾选. 紧接着删除子任务2,发现原本子任务3勾选消失了 查看里面保存勾选的值,的的确确有两 ...
- html下拉式列表框多选,基于jQuery UI的多项选择下拉列表框组件
jquery.multiselect.js是一款基于jQuery UI的多项选择下拉列表框组件.该下拉列表功能强大,可以实现多选和单选,可制作打开和关闭下拉框时的炫酷动画效果,提供丰富的回调函数和其它 ...
- 文章中嵌入代码块_如何在您的文章中嵌入多项选择测验问题
文章中嵌入代码块 In my experience, supplementing study with practical exercises greatly improves my understa ...
- 职称计算机提前考试试卷,职称计算机考试多项选择考试卷模拟考^试题
<职称计算机考试多项选择考试卷模拟考^试题>由会员分享,可在线阅读,更多相关<职称计算机考试多项选择考试卷模拟考^试题(8页珍藏版)>请在人人文库网上搜索. 1.姓名:____ ...
- CheckBox jsp+javaScript多项选择checkbox取值实现
<script language="javaScript"> //全选或全部不选 function checkall(){ var isChecked=(documen ...
最新文章
- 手机版的python-手机版python
- SAP: 如何取物料主数据的特性值
- Python的Super方法
- 8.2.4临时表和正式表
- 在Eclipse中运行hadoop程序
- 局部类型 之 部分方法
- Javascript引擎单线程机制及setTimeout执行原理说明
- 如何传入比较器_typescript专题(五) 装饰器
- java斗地主发牌源码
- 【SpringBoot_ANNOTATIONS】自动装配 05 @Profile环境搭建
- docker批量导出导入本地镜像
- windows服务器迁到_windows服务器搬迁指南
- 分享一组漂亮的html5后台登陆模板
- css控制文本超出省略(单行、两行、多行)
- OBS 基础 16 如何在CMake中添加新的lib库、头文件等
- 编译原理——语法分析器(C/C++代码实现)
- geek_2013年How-To Geek的节日礼物指南:男孩,女孩,极客和有情机器人的书籍
- 长春SEO优化:SEO优化开篇——内容优化
- 中南民族大学 计算机组成原理实验报告,计算机系统结构实验报告(中南民族大学)...
- 2020年Java语言发展现状