第五章 用户界面基础(RadioButton)

参考资料:
《Android应用程序开发》ISBN 9787302283164
参考软件:
Android Studio、Eclipse+ADT、Android SDK、JDK
用户界面基础(接上节内容)
五、RadioButton
RadioButton(单选按钮)在Android开发中应用的非常广泛,比如一些选择项的时候,会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在RadioButton没有被选中时,用户能够按下或点击来选中它。但是,与复选框相反,用户一旦选中就不能够取消选中。
实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听
下面的具体的例子:
MainActivity.java
1. package com.android.radiobutton;
2.
3. import android.app.Activity;
4. import android.os.Bundle;
5. import android.widget.RadioGroup;
6. import android.widget.Toast;
7.
8. public class MainActivity extends Activity {
9.
10. //声明RadioGroup
11. RadioGroup raGroup1,raGroup2;
12. @Override
13. public void onCreate(Bundle savedInstanceState) {
14. super.onCreate(savedInstanceState);
15. setContentView(R.layout.main);
16.
17. //通过findViewById获得RadioGroup对象
18. raGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
19. //添加事件监听器
20. raGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
21.
22. @Override
23. public void onCheckedChanged(RadioGroup group, int checkedId) {
24. // TODO Auto-generated method stub
25. if(checkedId==R.id.radioBtn1){
26. Toast.makeText(MainActivity.this, "你来自广东省", Toast.LENGTH_LONG).show();
27. }
28. else if(checkedId==R.id.radioBtn2){
29. Toast.makeText(MainActivity.this, "你来自广西省", Toast.LENGTH_LONG).show();
30. }
31. else{
32. Toast.makeText(MainActivity.this, "你来自湖南省", Toast.LENGTH_LONG).show();
33. }
34. }
35. });
36. raGroup2=(RadioGroup)findViewById(R.id.radioGroup2);
37. raGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
38.
39. @Override
40. public void onCheckedChanged(RadioGroup group, int checkedId) {
41. // TODO Auto-generated method stub
42. if(checkedId==R.id.radioBtn4){
43. Toast.makeText(MainActivity.this, "你的性别是男", Toast.LENGTH_LONG).show();
44. }
45. else {
46. Toast.makeText(MainActivity.this, "你的性别是女", Toast.LENGTH_LONG).show();
47. }
48. }
49. });
50. }
51.}
main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. >
7. <TextView
8. android:layout_width="fill_parent"
9. android:layout_height="wrap_content"
10. android:text="@string/hello1"
11. />
12. <RadioGroup
13. android:id="@+id/radioGroup1"
14. android:layout_width="wrap_content"
15. android:layout_height="wrap_content"
16. android:orientation="vertical"
17. >
18. <RadioButton
19. android:id="@+id/radioBtn1"
20. android:layout_width="wrap_content"
21. android:layout_height="wrap_content"
22. android:text="@string/radioBtn1"
23. />
24. <RadioButton
25. android:id="@+id/radioBtn2"
26. android:layout_width="wrap_content"
27. android:layout_height="wrap_content"
28. android:text="@string/radioBtn2"
29. />
30. <RadioButton
31. android:id="@+id/radioBtn3"
32. android:layout_width="wrap_content"
33. android:layout_height="wrap_content"
34. android:text="@string/radioBtn3"
35. />
36. </RadioGroup>
37. <!-- 在两个RadioGroup之间画条横线 -->
38. <View
39. android:layout_width="match_parent"
40. android:layout_height="1dp"
41. android:background="#ffffff"
42. />
43. <TextView
44. android:layout_width="fill_parent"
45. android:layout_height="wrap_content"
46. android:text="@string/hello2"
47. />
48. <RadioGroup
49. android:id="@+id/radioGroup2"
50. android:layout_width="wrap_content"
51. android:layout_height="wrap_content"
52. android:orientation="vertical"
53. >
54. <RadioButton
55. android:id="@+id/radioBtn4"
56. android:layout_width="wrap_content"
57. android:layout_height="wrap_content"
58. android:text="@string/radioBtn4"
59. android:textColor="#ffffff"
60. />
61. <RadioButton
62. android:id="@+id/radioBtn5"
63. android:layout_width="wrap_content"
64. android:layout_height="wrap_content"
65. android:text="@string/radioBtn5"
66. />
67. </RadioGroup>
68.</LinearLayout>
strings.xml
1. <?xmlversion="1.0"encoding="utf-8"?>
2. <resources>
3. <stringname="hello1">你来自哪个省</string>
4. <stringname="hello2">你的性别是</string>
5. <stringname="app_name">单选按钮测试</string>
6. <stringname="radioBtn1">广东</string>
7. <stringname="radioBtn2">广西</string>
8. <stringname="radioBtn3">湖南</string>
9. <stringname="radioBtn4">男</string>
10. <stringname="radioBtn5">女</string>
11.</resources>
效果图:

RadioButton的另一种效果:

要实现上面的效果,只要在main.xml布局文件中的<RadioButton/>加入android:button="@null"
android:drawableRight="@android:drawable/btn_radio"即可