Android控件之RadioButton

RadioButton和RadioGroup的关系:

1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器

2、每个RadioGroup中的RadioButton同时只能有一个被选中

3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中

4、大部分场合下,一个RadioGroup中至少有2个RadioButton

5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置

XML布局:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical">
 5 
 6     <RadioGroup
 7         android:id="@+id/group"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         >
11 
12 
13         <RadioButton
14             android:id="@+id/boy"
15             android:checked="true"//设置这个RadioButton是默认选中
16             android:layout_width="wrap_content"
17             android:layout_height="wrap_content"
18             android:text="男" />
19 
20         <RadioButton
21             android:id="@+id/girl"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:text="女" />
25 
26 
27     </RadioGroup>
28 
29     <TextView
30         android:id="@+id/te"
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content"
33         android:text="男"
34         />
35 
36 </LinearLayout>

java代码:

 1 package com.contentprovide.liuliu.radiogroup;
 2 
 3 import android.support.annotation.IdRes;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.widget.RadioButton;
 7 import android.widget.RadioGroup;
 8 import android.widget.TextView;
 9 
10 public class MainActivity extends AppCompatActivity {
11     RadioGroup group;
12     RadioButton check;
13     TextView te;
14 
15     RadioButton boy,girl;
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         group = (RadioGroup) findViewById(R.id.group);
22         te = (TextView) findViewById(R.id.te);
23 
24         boy = (RadioButton) findViewById(R.id.boy);
25         girl = (RadioButton) findViewById(R.id.girl);
26 
27 
28 
29         group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
30             @Override
31             public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
32 //                方法一
33 //                check = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
34 //                te.setText(check.getText().toString());
35 
36 //                方法二
37                 if(boy.getId()==i){
38                     te.setText(boy.getText().toString());
39                 }else if(girl.getId()==i){
40                     te.setText(girl.getText().toString());
41                 }
42 
43 
44             }
45         });
46 
47 
48     }
49 }

代码很少,两种方法也差不多,注意对一下id名,没有备注也比较容易看懂

原文地址:https://www.cnblogs.com/lyd447113735/p/8242262.html