Android基礎教程(七)之----單選項框RadioGroup的綜合應用

你們好,咱們今天這一節要介紹的是RadioGroup 的組事件.RadioGroup 可將各自不一樣的RadioButton ,設限於同一個Radio 按鈕組,同一個RadioGroup 組裏的按鈕,只能作出單一選擇(單選題).

首先,咱們先設計一個TextView Widget ,以及一個RadioGroup ,並將該RadioGroup 內放置兩個RadioButton ,默認爲都不選擇,在程序運行階段,利用onCheckedChanged 做爲啓動事件裝置,讓User選擇其中一個按鈕,顯示被選擇的內容,最的將RadioButton 的選項文字顯示於TextView 當中.

下面咱們看一下效果圖:





下面是涉及的相關代碼:

string.xml:
java

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.   <string name="hello">Hello World, RadioGroupDemo</string>
  4.   <string name="app_name">RadioGroupDemo</string>
  5.   <string name="tr_radio_op1">帥哥</string>
  6.   <string name="tr_radio_op2">美女</string>
  7.   <string name="str_radio_question1">請問你是?</string>
  8. </resources>
複製代碼
主佈局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.   <TextView
  9.     android:id="@+id/myTextView"
  10.     android:layout_width="228px"
  11.     android:layout_height="49px"
  12.     android:text="@string/str_radio_question1"
  13.     android:textSize="30sp"  
  14.   />  
  15.   <!--創建一個RadioGroup -->
  16.   <RadioGroup
  17.     android:id="@+id/myRadioGroup"
  18.     android:layout_width="137px"
  19.     android:layout_height="216px"
  20.     android:orientation="vertical"
  21.     >
  22.     <!--第一個RadioButton -->
  23.     <RadioButton
  24.       android:id="@+id/myRadioButton1"
  25.       android:layout_width="wrap_content"
  26.       android:layout_height="wrap_content"
  27.       android:text="@string/tr_radio_op1"
  28.     />
  29.     <!--第二個RadioButton -->
  30.     <RadioButton
  31.       android:id="@+id/myRadioButton2"
  32.       android:layout_width="wrap_content"
  33.       android:layout_height="wrap_content"
  34.       android:text="@string/tr_radio_op2"
  35.     />
  36.     </RadioGroup>    
  37. </LinearLayout>
複製代碼
最後是主控制程序RadioGroupDemo.java:
  1. package com.android.test;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.RadioButton;
  5. import android.widget.RadioGroup;
  6. import android.widget.TextView;

  7. public class RadioGroupDemo extends Activity
  8. {
  9.   public TextView mTextView1;
  10.   public RadioGroup mRadioGroup1;
  11.   public RadioButton mRadio1,mRadio2;

  12.   public void onCreate(Bundle savedInstanceState)
  13.   {
  14.     super.onCreate(savedInstanceState);
  15.     setContentView(R.layout.main);
  16.     
  17.     /*取得 TextView、RadioGroup、RadioButton對象*/
  18.     mTextView1 = (TextView) findViewById(R.id.myTextView);
  19.     mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
  20.     mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
  21.     mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);
  22.     
  23.     /*RadioGroup用OnCheckedChangeListener來運行*/
  24.     mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
  25.   }
  26.   
  27.   private RadioGroup.OnCheckedChangeListener mChangeRadio = new
  28.           RadioGroup.OnCheckedChangeListener()
  29.   {
  30.     @Override
  31.     public void onCheckedChanged(RadioGroup group, int checkedId)
  32.     {
  33.       // TODO Auto-generated method stub
  34.       if(checkedId==mRadio1.getId())
  35.       {
  36.         /*把mRadio1的內容傳到mTextView1*/
  37.         mTextView1.setText(mRadio1.getText());
  38.       }
  39.       else if(checkedId==mRadio2.getId())
  40.       {
  41.         /*把mRadio2的內容傳到mTextView1*/
  42.         mTextView1.setText(mRadio2.getText());
  43.       }      
  44.     }
  45.   };
  46. }
複製代碼
運行RadioGroupDemo.java ,將獲得以上效果。
相關文章
相關標籤/搜索