歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:RadioButton和CheckBox淺析

Android開發教程:RadioButton和CheckBox淺析

日期:2017/3/1 10:36:41   编辑:Linux編程
一.RadioButton單選按鈕

RadioButton(單選按鈕)在Android開發中應用的非常廣泛,比如一些選擇項的時候,會用到單選按鈕。它是一種單個圓形單選框雙狀態的按鈕,可以選擇或不選擇。在RadioButton沒有被選中時,用戶能夠按下或點擊來選中它。但是,與復選框相反,用戶一旦選中就不能夠取消選中。

實現RadioButton由兩部分組成,也就是RadioButton和RadioGroup配合使用.RadioGroup是單選組合框,可以容納多個RadioButton的容器.在沒有RadioGroup的情況下,RadioButton可以全部都選中;當多個RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個。並用setOnCheckedChangeListener來對單選按鈕進行監聽

下面的具體的例子:

MainActivity.java

  1. package com.android.radiobutton;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.RadioGroup;
  5. import android.widget.Toast;
  6. public class MainActivity extends Activity {
  7. //聲明RadioGroup
  8. RadioGroup raGroup1,raGroup2;
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. //通過findViewById獲得RadioGroup對象
  14. raGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
  15. //添加事件監聽器
  16. raGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  17. @Override
  18. public void onCheckedChanged(RadioGroup group, int checkedId) {
  19. // TODO Auto-generated method stub
  20. if(checkedId==R.id.radioBtn1){
  21. Toast.makeText(MainActivity.this, "你來自廣東省", Toast.LENGTH_LONG).show();
  22. }
  23. else if(checkedId==R.id.radioBtn2){
  24. Toast.makeText(MainActivity.this, "你來自廣西省", Toast.LENGTH_LONG).show();
  25. }
  26. else{
  27. Toast.makeText(MainActivity.this, "你來自湖南省", Toast.LENGTH_LONG).show();
  28. }
  29. }
  30. });
  31. raGroup2=(RadioGroup)findViewById(R.id.radioGroup2);
  32. raGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  33. @Override
  34. public void onCheckedChanged(RadioGroup group, int checkedId) {
  35. // TODO Auto-generated method stub
  36. if(checkedId==R.id.radioBtn4){
  37. Toast.makeText(MainActivity.this, "你的性別是男", Toast.LENGTH_LONG).show();
  38. }
  39. else {
  40. Toast.makeText(MainActivity.this, "你的性別是女", Toast.LENGTH_LONG).show();
  41. }
  42. }
  43. });
  44. }
  45. }

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. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello1">你來自哪個省</string>
  4. <string name="hello2">你的性別是</string>
  5. <string name="app_name">單選按鈕測試</string>
  6. <string name="radioBtn1">廣東</string>
  7. <string name="radioBtn2">廣西</string>
  8. <string name="radioBtn3">湖南</string>
  9. <string name="radioBtn4">男</string>
  10. <string name="radioBtn5">女</string>
  11. </resources>

效果圖:

650) this.width=650;" height=120>

RadioButton的另一種效果:

650) this.width=650;" height=120>

Copyright © Linux教程網 All Rights Reserved