RadioButton 类结构图: RadioButton很好理解就是我们做Web开发里的单选按钮,需要注意的是我们在做Web开发需要将单选按钮的名字设置成一样,在这里Android开发,我们需要将两个或者更多的RadioButton放在一个RadioGroup里实战演练:1、如何设置RadioButton的对齐方式:通过android:orientation其值有:vertical、horizontal垂直:
- <RadioGroup android:id="@+id/raGroup01"
-
- android:layout_width="fill_parent" android:layout_height="wrap_content"
-
- android:orientation="vertical">
-
- <RadioButton android:id="@+id/raBtn01" android:text="男" />
-
- <RadioButton android:id="@+id/raBtn02" android:text="女" />
-
- </RadioGroup>
效果:水平:
- <RadioGroup android:id="@+id/raGroup02"
-
- android:layout_width="fill_parent" android:layout_height="wrap_content"
-
- android:orientation="horizontal">
-
- <RadioButton android:id="@+id/rautf" android:text="UTF" />
-
- <RadioButton android:id="@+id/ragbk" android:text="GBK" />
-
- </RadioGroup>
效果:2、如何让RadioButton设置成选中android:checkedButton="@+id/rautf"
- <RadioGroup android:id="@+id/raGroup02"
-
- android:layout_width="fill_parent" android:layout_height="wrap_content"
-
- android:orientation="horizontal"
-
- android:checkedButton="@+id/rautf">
-
- <RadioButton android:id="@+id/rautf" android:text="UTF" />
-
- <RadioButton android:id="@+id/ragbk" android:text="GBK" />
-
- </RadioGroup>
效果: 添加监听器:
- radioButton01 = (RadioButton) findViewById(R.id.raBtn01);
-
-
-
- radioButton01.setOnClickListener(new OnClickListener() {
-
- public void onClick(View v) {
-
- String text = radioButton01.getText().toString();
-
- System.out.println(text);
-
-
-
- }
-
- });
-
-