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. nginx基础内容

    1.配置文件结构图 2.作用1:静态文件服务器 http { server { listen ; location / { root /data/www; } location /images/ { ...

  2. Balking Pattern不需要就不用做

    word自动保存功能,如果文档被修改了,后台线程每隔一段时间会自动执行保存功能,但是如果用户在自动保存之前用Ctrl+S手动保存呢?自动保存还会执行吗?答案是不会,因为这个操作时不需要重复做的. pu ...

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

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

  4. C# - Finalize 和 Dispose

    重要: https://www.cnblogs.com/Jessy/articles/2552839.html https://blog.csdn.net/daxia666/article/detai ...

  5. Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)F3 - Lightsabers (hard)

    题意:n个数字1-m,问取k个组成的set方案数 题解:假设某个数出现k次,那么生成函数为\(1+x+...+x^k\),那么假设第i个数出现ai次,结果就是\(\sum_{i=1}^m(1+x+.. ...

  6. 在Rancher2中部署Docker-Registry仓库服务

    1.docker登陆认证 [root@master ~]# vim /etc/docker/daemon.json { "insecure-registries": [" ...

  7. js--判断当前环境是否为app环境

    /** *判断是否是app环境 */ function getIsApp () { var ua = navigator.userAgent.toLowerCase(); if (ua.match(/ ...

  8. JAVA POI XSSFWorkbook导出扩展名为xlsx的Excel,附带weblogic 项目导出Excel文件错误的解决方案

    现在很多系统都有导出excel的功能,总结一下自己之前写的,希望能帮到其他人,这里我用的是XSSFWorkbook,我们项目在winsang 用的Tomcat,LInux上用的weblogic服务器, ...

  9. Docker系列(五):Docker网络机制(上)

    Linux路由机制打通网络 路由机制是效率最好的 docker128上修改Docker0的网络地址,与docker130不冲突 vi /usr/lib/systemd/system/docker.se ...

  10. ASP.NET自定义Validform的datatype

    1.定义 <script type="text/javascript"> $(function () { $("#aa").Validform({ ...