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. Reverses CodeForces - 906E (最小回文分解)

    题意: 给你两个串s和t,其中t是由s中选择若干个不相交的区间翻转得到的,现在要求求出最少的翻转次数以及给出方案. 1≤|s|=|t|≤500000 题解: 我们将两个字符串合成成T=s1t1s2t2 ...

  2. xwiki系统 知识库 xwiki

    1.下载tomcat tar  -zxvf  apache-tomcat-8.0.14.tar.gz mv apache-tomcat-8.0.14 tomcat-xwiki-8.0 2.下载xwik ...

  3. 【转载】C# 开源库大全非常好

    原文地址:http://m.blog.csdn.net/woddle/article/details/37311877 C#开源大全 商业协作和项目管理平台-TeamLab 网络视频会议软件-VMuk ...

  4. 2018-10-8-3分钟教你搭建-gitea-在-Centos-服务器

    title author date CreateTime categories 3分钟教你搭建 gitea 在 Centos 服务器 lindexi 2018-10-08 09:54:39 +0800 ...

  5. ionic js 侧栏菜单 把主要内容区域从一边拖动到另一边,来让左侧或右侧的侧栏菜单进行切换

    ionic 侧栏菜单 一个容器元素包含侧边菜单和主要内容.通过把主要内容区域从一边拖动到另一边,来让左侧或右侧的侧栏菜单进行切换. 效果图如下所示: 用法 要使用侧栏菜单,添加一个父元素<ion ...

  6. digitalpersona 开发(系统托盘,监听指纹扫描)

    其实很简单,主要是生成  DPFPCapture  对象时,设置他的优先级就可以了. (改成High的话,发布后,windows系统会认为你是病毒.....) C# //设置优先级,这个就是系统托盘后 ...

  7. DEV的GridControl控件的选中列属性设置高光

    设置Run Designer=>Views=> OptionsSelection下面的: EnableAppearanceFocusedCell = False,//鼠标移开,失去焦点,仍 ...

  8. du和df

    du,disk usage,是通过搜索文件来计算每个文件的大小然后累加,du能看到的文件只是一些当前存在 的,没有被删除的.(-s:summarize 仅显示总计,只列出最后加总的值) df,disk ...

  9. Android开发 了解ViewModel

    前言 ViewModel是google推出的一个数据处理框架,ViewModel类是被设计用来以可感知生命周期的方式存储和管理 UI 相关数据ViewModel中数据会一直存活即使 activity ...

  10. Mysql: [Warning] Using a password on the command line interface can be insecure

    mysql: [Warning] Using a password on the command line interface can be insecure MySQL 5.6 警告信息 comma ...