1. 程式人生 > >2.常用控制元件:RadioGroup和RadioButton

2.常用控制元件:RadioGroup和RadioButton

常用控制元件:RadioGroup和RadioButton

一.核心程式碼:

檢視:

在相應的Activity佈局檔案中宣告<RadioGroup/><RadioButton/>

<RadioGroup
	android:id="@+id/genderGroup"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"
    >
    <RadioButton
    	android:id="@+id/femaleButton"
 	    android:layout_width="wrap_content" 
  	    android:layout_height="wrap_content" 
  	    android:text="@string/female"
  	    />
    <RadioButton
    	android:id="@+id/maleButton"
 	    android:layout_width="wrap_content" 
  	    android:layout_height="wrap_content" 
  	    android:text="@string/male"
  	    />
</RadioGroup>


資料:

Activity中可以通過View android.app.Activity.findViewById(int id)函式返回RadioGroup物件。

Activity中可以通過View android.app.Activity.findViewById(int id)函式返回RadioButton物件。

為RadioGroup物件設定監聽器genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()) 建立一個匿名內部類並實現android.widget.RadioGroup.OnCheckedChangeListener
介面,作為引數傳入RadioGroup物件的setOnCheckedChangeListener(OnCheckedChangeListener listener)函式中。

        //通過控制元件的ID來得到代表控制元件的物件
        genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
        femaleButton = (RadioButton)findViewById(R.id.femaleButton);
        maleButton = (RadioButton)findViewById(R.id.maleButton);
        //為RadioGroup設定監聽器,需要注意的是,這裡的監聽器和Button控制元件的監聽器有所不同
        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(femaleButton.getId() == checkedId){
					System.out.println("famale");
					Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();
				}
				else if(maleButton.getId() == checkedId)
				{
					System.out.println("male");
				}
			}
		});


二.控制元件圖例:


三.具體應用:

XML檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<RadioGroup
	android:id="@+id/genderGroup"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"
    >
    <RadioButton
    	android:id="@+id/femaleButton"
 	    android:layout_width="wrap_content" 
  	    android:layout_height="wrap_content" 
  	    android:text="@string/female"
  	    />
    <RadioButton
    	android:id="@+id/maleButton"
 	    android:layout_width="wrap_content" 
  	    android:layout_height="wrap_content" 
  	    android:text="@string/male"
  	    />
</RadioGroup>
</LinearLayout>


Activity.java檔案:


package mars.activity07;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioTest extends Activity {
    /** Called when the activity is first created. */
	//對控制元件物件進行宣告
	private RadioGroup genderGroup = null;
	private RadioButton femaleButton = null;
	private RadioButton maleButton = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio);
        //通過控制元件的ID來得到代表控制元件的物件
        genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
        femaleButton = (RadioButton)findViewById(R.id.femaleButton);
        maleButton = (RadioButton)findViewById(R.id.maleButton);
        //為RadioGroup設定監聽器,需要注意的是,這裡的監聽器和Button控制元件的監聽器有所不同
        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(femaleButton.getId() == checkedId){
					System.out.println("famale");
					Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();
				}
				else if(maleButton.getId() == checkedId)
				{
					System.out.println("male");
				}
			}
		});
        
        
    }
    
}