Android控件RadioButton实现多选一功能

Android控件RadioButton实现多选一功能,第1张

概述RadioButton实现多选一功能的方法,具体内容如下一、简介二、RadioButton实现多选一方法

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,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,Toast.LENGTH_LONG).show();

吐司

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

总结

以上是内存溢出为你收集整理的Android控件RadioButton实现多选一功能全部内容,希望文章能够帮你解决Android控件RadioButton实现多选一功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1144897.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存