Android控件RadioButton实现多选一功能

所属分类: 软件编程 / Android 阅读数: 42
收藏 0 赞 0 分享

RadioButton实现多选一功能的方法,具体内容如下

一、简介

二、RadioButton实现多选一方法

1、将多个RadioButton放在一个RadioGroup里面

<RadioGroup
  android:id="@+id/radioGroup1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >

  <RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="男"
   android:textColor="#FFFFFF" />

  <RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="女"
   android:textColor="#FFFFFF" />
 </RadioGroup>

2、在RadioGroup里面取出每个RadioButton

public void onClick(View v) {
    // TODO Auto-generated method stub
    int len = radioGroup1.getChildCount();
    for (int i = 0; i < len; i++) {
     RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);11     }
   }

3、检查每个RadioButton是否被选取

 if (radio.isChecked()) {

      break;
     } 

4、取出被选取的那个RadioButton里面的值

Toast.makeText(Activity01.this, radio.getText(),
        Toast.LENGTH_LONG).show();

三、代码实例

效果图:

代码:

fry.Activity01

package fry;

import com.example.RadioButtonDemo1.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Activity01 extends Activity {
 private Button btn_chooseGender;
 private RadioGroup radioGroup1;
 private TextView tv_answer;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity01);

  btn_chooseGender = (Button) findViewById(R.id.btn_chooseGender);
  radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
  tv_answer = (TextView) findViewById(R.id.tv_answer);
  /*
   * RadioButton实现多选一方法
   * 1、将多个RadioButton放在一个RadioGroup里面
   * 2、在RadioGroup里面取出每个RadioButton 
   * 3、检查每个RadioButton是否被选取
   * 4、取出被选取的那个RadioButton里面的值
   */
  btn_chooseGender.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    int len = radioGroup1.getChildCount();
    for (int i = 0; i < len; i++) {
     RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);
     if (radio.isChecked()) {
      Toast.makeText(Activity01.this, radio.getText(),
        Toast.LENGTH_LONG).show();
      break;
     }
    }
   }
  });
 }
}

/RadioButtonDemo1/res/layout/activity01.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@android:color/black"
 android:orientation="vertical" >

 <TextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="性别"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:layout_gravity="center_horizontal"
  android:textColor="#FFFFFF" />

 <RadioGroup
  android:id="@+id/radioGroup1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >

  <RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="男"
   android:textColor="#FFFFFF" />

  <RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="女"
   android:textColor="#FFFFFF" />
 </RadioGroup>

 <Button 
  android:id="@+id/btn_chooseGender"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="选择性别"
  android:textColor="#FFFFFF" />
  />
  
 <TextView
  android:id="@+id/tv_answer"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text=""
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:layout_gravity="center_horizontal"
  android:textColor="#FFFFFF" />
</LinearLayout>

四、收获

1、

android:textColor="#FFFFFF"

设置颜色,直接用#FFFFFF

2、

android:layout_gravity="center_horizontal"

文字居中显示

3、

RadioButton在RadioGroup里面实现多选一

4、

android:background="@android:color/black"

设置黑色,系统自带颜色

5、

int len = radioGroup1.getChildCount();

RadioGroup获取孩子数量

6、

RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);

RadioGroup获取孩子

7、

if (radio.isChecked())

判断RadioButton是否被选取

8、

Toast.makeText(Activity01.this, radio.getText(),Toast.LENGTH_LONG).show();

吐司

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

android开发之Json文件的读写的示例代码

这篇文章主要介绍了android开发之Json文件的读写的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android7.0指纹服务FingerprintService实例介绍

这篇文章主要介绍了Android7.0指纹服务FingerprintService介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Android JNI处理图片实现黑白滤镜的方法

这篇文章主要介绍了Android JNI处理图片实现黑白滤镜的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android引入OpenCV的示例

本篇文章主要介绍了Android引入OpenCV的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android Zip解压缩工具类分享

这篇文章主要为大家详细介绍了Android Zip解压缩工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Interval

这篇文章主要为大家详细介绍了Android RxJava创建操作符Interval的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

5分钟快速实现Android爆炸破碎酷炫动画特效的示例

本篇文章主要介绍了5分钟快速实现Android爆炸破碎酷炫动效的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android 指纹功能实例代码

本文通过一个demo给大家介绍了android指纹功能,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现倒计时CountDownTimer使用详解

这篇文章主要为大家详细介绍了Android实现倒计时CountDownTimer的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Timer的方法

这篇文章主要为大家详细介绍了Android RxJava创建操作符Timer的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多