Android控件之Radiobutton与RadioGroup

RadioButton 是一个单选控件,在一个RadioGroup中,各个RadioButton是互斥的

XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >
	<RadioGroup 
	    android:id="@+id/RadioGroup1"
	    android:layout_width="fill_parent"
	    android:layout_height="match_parent"	    
	    >
    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        
        android:layout_marginTop="42dp"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/radioButton1"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/radioButton2"
        android:text="RadioButton" />
	</RadioGroup>
</RelativeLayout>


使用OnCheckedChangeListener监听:

package com.example.textview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {

	
	private RadioGroup RadioGroup1=null;
	private RadioButton RadioButton1=null;
	private RadioButton RadioButton2=null;
	private RadioButton RadioButton3=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);		
		RadioGroup1=(RadioGroup)findViewById(R.id.RadioGroup1);
		RadioButton1=(RadioButton)findViewById(R.id.radioButton1);
		RadioButton2=(RadioButton)findViewById(R.id.radioButton2);
		RadioButton3=(RadioButton)findViewById(R.id.radioButton3);
		
		RadioGroup1.setOnCheckedChangeListener(listener);
		
	}
	
	
	private OnCheckedChangeListener listener = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			
			
			if(checkedId==RadioButton1.getId())
			{
				System.out.println("button 1");
			}
			else if(checkedId==RadioButton2.getId())
			{
				System.out.println("button 2");
			}
			else if(checkedId==RadioButton3.getId())
			{				
				System.out.println("button 3");				
			}
			
		}
	};

	

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>