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();

吐司

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

02-06 01:21