RadioGroup的使用方法

Activity

 

 

package cn.edu.radiogroup;

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

public class RadioGroupActivity extends Activity {
    /** Called when the activity is first created. */
	private RadioGroup rgpParent;
	private RadioButton rgp1;
	private RadioButton rgp2;
	private RadioButton rgp3;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        rgpParent=(RadioGroup)this.findViewById(R.id.grp);
        rgp1=(RadioButton)this.findViewById(R.id.one);
        rgp2=(RadioButton)this.findViewById(R.id.two);
        rgp3=(RadioButton)this.findViewById(R.id.three);
        rgpParent.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				// TODO Auto-generated method stub
				if(arg1==rgp1.getId()){
					Toast.makeText(RadioGroupActivity.this, "Frist Radio be Selected", 10).show();
				}else if(arg1==rgp2.getId()){
					Toast.makeText(RadioGroupActivity.this, "Second Radio be Selected", 10).show();
				}else {
					Toast.makeText(RadioGroupActivity.this, "Third Radio be Selected", 10).show();
				}
			}
        	
        });
    }
}

 

布局文件

<?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">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />

	<RadioGroup android:id="@+id/grp" android:layout_width="fill_parent"
			android:layout_height="wrap_content">
		<RadioButton android:id="@+id/one" 
			android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:text="one" />

		<RadioButton android:id="@+id/two" android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:text="two" />

		<RadioButton android:id="@+id/three" android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:text="three" />
	</RadioGroup>
</LinearLayout>
 

 

你可能感兴趣的:(Radio)