android控件07 RadioGroup

1)/res/layout/main.xml

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

	<RadioGroup 
    	android:id="@+id/radiogroup" 
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"  
    	android:orientation="vertical"
       >
      <RadioButton 
          	android:id="@+id/radiobutton1"
          	android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
			android:text="Radio按鍵1"	
			android:checked="true"
       />
      <RadioButton 
          	android:id="@+id/radiobutton2"
          	android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
			android:text="Radio按鍵2"	
       />
      <RadioButton 
          	android:id="@+id/radiobutton3"
          	android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
			android:text="Radio按鍵3"	
       />
	   </RadioGroup>
</LinearLayout>

2)com.sxt.RadioGroupActivity.java

package com.sxt;

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

public class RadioGroupActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RadioGroup radioGroup = (RadioGroup)this.findViewById(R.id.radiogroup);
        final RadioButton radioButton1 = (RadioButton)this.findViewById(R.id.radiobutton1);
        final RadioButton radioButton2 = (RadioButton)this.findViewById(R.id.radiobutton2);
        final RadioButton radioButton3 = (RadioButton)this.findViewById(R.id.radiobutton3);
        
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(checkedId == radioButton1.getId())
				{
					Toast.makeText(RadioGroupActivity.this, "radio button id1:"+checkedId, Toast.LENGTH_SHORT).show();
				}
				if(checkedId == radioButton2.getId())
				{
					Toast.makeText(RadioGroupActivity.this, "radio button id2:"+checkedId, Toast.LENGTH_SHORT).show();
				}
				if(checkedId == radioButton3.getId())
				{
					Toast.makeText(RadioGroupActivity.this, "radio button id3:"+checkedId, Toast.LENGTH_SHORT).show();
				}
			}
		});
    }
}
3)如圖



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章