1、相关简介

  RadioButton需要和RadioGroup结合使用,在RadioGroup设置布局方式!

  Checkbox是单独使用,本文为了方便放在了RadioGroup中!

2、简单使用

  方法说明:

final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
radioGroup.getChildCount() //获取子控件数量
RadioButton radioButton = (RadioButton)radioGroup.getChildAt(i); //获取子控件
radioButton.isChecked() //获取按钮选择状态
radioButton.getText() //获取按钮文本

  xml布局文件

        <RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio11"
android:text="男"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio22"
android:text="女"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio33"
android:text="太监"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioBtn"
android:text="提交"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/boxGroup"
android:orientation="vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box11"
android:text="男"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box22"
android:text="女"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/boxBtn"
android:text="提交"/>

  Java文件:

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Set up the login form. final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
// 方法一
// radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(RadioGroup radioGroup, int i) {
// RadioButton radioButton = (RadioButton)findViewById(i);
// Toast.makeText(getApplicationContext(),"你选择了"+radioButton.getText(),Toast.LENGTH_LONG).show();
// }
// }); // 方法二
Button button = (Button)findViewById(R.id.radioBtn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0;i < radioGroup.getChildCount();i++){
RadioButton radioButton = (RadioButton)radioGroup.getChildAt(i);
if (radioButton.isChecked()){
Toast.makeText(getApplicationContext(),"你选择了"+radioButton.getText(),Toast.LENGTH_SHORT).show(); }
}
}
}); Button button1 = (Button)findViewById(R.id.boxBtn);
final RadioGroup boxGroup = (RadioGroup)findViewById(R.id.boxGroup);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String choos = "";
for (int i=0;i<boxGroup.getChildCount();i++){
CheckBox checkBox = (CheckBox)boxGroup.getChildAt(i);
if (checkBox.isChecked()){
choos +=checkBox.getText().toString();
}else {
if (choos.indexOf(checkBox.getText().toString())!=-1){
choos.substring(choos.indexOf(checkBox.getText().toString()));
}
}
if (i==boxGroup.getChildCount()-1){
Toast.makeText(getApplicationContext(),"选了"+choos,Toast.LENGTH_SHORT).show();
}
}
}
}); }
}

效果图

Android基础控件单选按钮RadioButton和Checkbox复选按钮的使用的更多相关文章

  1. Android基础控件ListView基础操作

    1.简介 基于Android基础控件ListView和自定义BaseAdapter适配器情况下,对ListView的数据删除和添加操作: public boolean add(E e) {//添加数据 ...

  2. android基础控件的使用

    控件在屏幕上位置的确定 通常情况下控件在屏幕上确定至少要连接两条线(一条水平,一条垂直) 如下图连接了四条线 辅助线 辅助线的调出: 水平辅助线:进入activity.xml的设计模式之后如下图 为了 ...

  3. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  4. 矩阵, 矩阵 , Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

  5. android基本控件学习-----RadioButton&CheckBox

    RadioButton(单选框)和CheckBox(复选框)讲解: 一.基本用法和事件处理 (1)RadioButton单选框,就是只能选择其中的一个,我们在使用的时候需要将RadioButton放到 ...

  6. android 基础控件 EditText

    EditText 简介: EditText 控件继承 TextView ,它有TextView的所有属性和方法,并且自身是可编辑的: extends TextView java.lang.Object ...

  7. Android 基础控件 TextView

    一TextView介绍: TextView是UI最基本的组件,使用TextView可以显示丰富的文本信息.设置添加TextView最常见的方法就是在xml中添加TextView元素,并指定属性.Tex ...

  8. Android基础控件TextClock和Chronometer的使用

    1.简介 DigitalClock, TextClock,AnalogClock,Chronometer其中DigitalClock和AnalogClock废弃了! TextClock是在Androi ...

  9. Android基础控件ProgressBar进度条的使用

    1.简介 ProgressBar继承与View类,直接子类有AbsSeekBar和ContentLoadingProgressBar, 其中AbsSeekBar的子类有SeekBar和RatingBa ...

随机推荐

  1. <面试题>学习面试

    1.代码中要修改不可变数据会出现什么问题? 抛出什么异常? 代码不会正常运行,抛出 TypeError 异常. # 比如修改元祖.会报错 TypeError: 'tuple' object does ...

  2. Win和Linux定时备份数据库

    项目的数据库需要每天备份,但是手动备份太麻烦而且容易忘,所以通过定时任务执行脚本备份数据库,服务器有Windows和Linux,所以两种都记录一下. 一.Windows 首先写好脚本,这里不多说,因为 ...

  3. JS事件 失焦事件(onblur)onblur事件与onfocus是相对事件,当光标离开当前获得聚焦对象的时候,触发onblur事件,同时执行被调用的程序。

    失焦事件(onblur) onblur事件与onfocus是相对事件,当光标离开当前获得聚焦对象的时候,触发onblur事件,同时执行被调用的程序. 如下代码, 网页中有用户和密码两个文本框.当前光标 ...

  4. PHP算法之按奇偶排序数组

    给定一个非负整数数组 A,返回一个数组,在该数组中, A 的所有偶数元素之后跟着所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入:[3,1,2,4]输出:[2,4,3,1]输出 ...

  5. c_ 数据结构_图_邻接矩阵

    程序主要实现了图的深度遍历和广度遍历. #include <stdio.h> #include <stdlib.h> #include <string.h> #de ...

  6. uoj49 轴仓库

    题意: n叠箱子排成一线,第i叠箱子坐标为xi,竖直方向叠着ai个箱子. 可以花费+1s左移或右移一位,也可以在瞬间搬起一个位置的箱子,或将怀里的有且仅有一个箱子放下. 任意选择起点s(可以不与xi重 ...

  7. VBS脚本完美实现开机延时启动

    目录 概述 vbs内容示例: vbs示例语句分析 自定义vbs脚本 一些问题和解决方法   概述 系统开机时,顺带自动启动了不少驱动程序,使得电脑开机后鼠标要呆滞许久.为了加快windows的开机速度 ...

  8. RabbitMQ 连接不上

    问题 [org.springframework.amqp.AmqpIOException: java.io.IOException] 解决 username: guest password: gues ...

  9. SQL Server 添加数据库没有权限等

    { 在安装好sql 后 第一次需要用windows 方式登陆 1.创建一个宁外一个登陆名登陆 在安全->登陆名 2.给此登陆属性的服务器角色添加sysadmin权限 //尽情享受!!! }

  10. 「题解」:$e$

    问题 B: $e$ 时间限制: 2 Sec  内存限制: 512 MB 题面 题面谢绝公开. 题解 话说一天考两个主席树这回事…… 正解可以叫树上主席树??(脸哥说也叫主席树上树???) 对于树上的每 ...