radiogroup多选_RadioButton实现多选⼀RadioButton实现多选⼀
⼀、简介
⼆、RadioButton实现多选⼀⽅法
1、将多个RadioButton放在⼀个RadioGroup⾥⾯
1
3 android:layout_width="match_parent"
4 android:layout_height="wrap_content" >
5
6
8 android:layout_height="wrap_content"
9 android:text="男"
10 android:textColor="#FFFFFF" />
11
12
14 android:layout_height="wrap_content"
15 android:text="⼥"
16 android:textColor="#FFFFFF" />
17
2、在RadioGroup⾥⾯取出每个RadioButton
1 public voidonClick(View v) {
2 //TODO Auto-generated method stub
3 int len =ChildCount();
4 for (int i = 0; i < len; i++) {
5 RadioButton radio =(RadioButton) ChildAt(i);11 }12 }
3、检查每个RadioButton是否被选取
1 if(radio.isChecked()) {4 break;5 }
4、取出被选取的那个RadioButton⾥⾯的值
1 Toast.makeText(Activity01.this, Text(),
2 Toast.LENGTH_LONG).show();
三、代码实例
效果图:
代码:
fry.Activity01
1 packagefry;2
ample.RadioButtonDemo1.R;4
5 importandroid.app.Activity;
6 importandroid.os.Bundle;
7 importandroid.view.View;8
importandroid.view.View.OnClickListener;9 importandroid.widget.Button;10 importandroid.widget.RadioButton;11 importandroid.widget.RadioGroup;12 importandroid.widget.TextView;13 importandroid.widget.Toast;14
15 public class Activity01 extendsActivity {16 privateButton btn_chooseGender;17 privateRadioGroup radioGroup1;18 privateTextView tv_answer;19
20 @Override21 protected voidonCreate(Bundle savedInstanceState) {22 //TODO Auto-generated method stub
Create(savedInstanceState);24 setContentView(R.layout.activity01);25
26 btn_chooseGender =(Button) findViewById(R.id.btn_chooseGender);27 radioGroup1 =(RadioGroup)
findViewById(R.id.radioGroup1);28 tv_answer =(TextView) findViewById(R.id.tv_answer);29 /*
30 * RadioButton实现多选⼀⽅法31 * 1、将多个RadioButton放在⼀个RadioGroup⾥⾯32 * 2、在RadioGroup⾥⾯取出每个RadioButton33 * 3、检查每个RadioButton是否被选取34 * 4、取出被选取的那个RadioButton⾥⾯的值35 */
36 btn_chooseGender.setOnClickListener(newOnClickListener() {37
38 @Override39 public voidonClick(View v) {40 //TODO Auto-generated method stub
android radiogroup41 int len =ChildCount();42 for (int i = 0; i < len; i++) {43 RadioButton radio =(RadioButton) ChildAt(i);44 if(radio.isChecked()) {45 Toast.makeText(Activity01.this, Text(),46
Toast.LENGTH_LONG).show();47 break;48 }49 }50 }51 });52 }53 }
/RadioButtonDemo1/res/l
1 <?xml version="1.0" encoding="utf-8"?>
2
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:background="@android:color/black"
6 android:orientation="vertical" >
7
8
10 android:layout_height="wrap_content"
11 android:text="性别"
12 android:textAppearance="?android:attr/textAppearanceLarge"
13 android:layout_gravity="center_horizontal"
14 android:textColor="#FFFFFF" />
15
16
18 android:layout_width="match_parent"
19 android:layout_height="wrap_content" >
20
21
23 android:layout_height="wrap_content"
24 android:text="男"
25 android:textColor="#FFFFFF" />
26
27
29 android:layout_height="wrap_content"
30 android:text="⼥"
31 android:textColor="#FFFFFF" />
32
33
34
36 android:layout_width="match_parent"
37 android:layout_height="wrap_content"
38 android:text="选择性别"
39 android:textColor="#FFFFFF" />
40 />
41
42
44 android:layout_width="match_parent"
45 android:layout_height="wrap_content"
46 android:text=""
47 android:textAppearance="?android:attr/textAppearanceLarge"
48 android:layout_gravity="center_horizontal"
49 android:textColor="#FFFFFF" />
50
四、收获
1、
android:textColor="#FFFFFF"
设置颜⾊,直接⽤#FFFFFF
2、
android:layout_gravity="center_horizontal"
⽂字居中显⽰
3、
RadioButton在RadioGroup⾥⾯实现多选⼀
4、
android:background="@android:color/black"
设置⿊⾊,系统⾃带颜⾊
5、
int len = ChildCount();
RadioGroup获取孩⼦数量
6、
RadioButton radio = (RadioButton) ChildAt(i);
RadioGroup获取孩⼦
7、
if (radio.isChecked())
判断RadioButton是否被选取
8、
Toast.makeText(Activity01.this, Text(),Toast.LENGTH_LONG).show();吐司

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。