Android初级之组件基础07-RadioButton/RadioGroup

关于RadioButton

RadioButton继承于CompoundButton,是TextView、Button的子孙类,其有两种状态(选中或不选中)。

常用属性:

 isChecked() 判断是否被选中,返回true或者false。
 performClick() 调用OnClickListener监听器,即相当于模拟一次单击事件。
 setChecked(boolean) 通过传入的布尔参数设置组件选中状态。
 toggle() 置放控件当前的状态。
 setOnCheckedChangeListener 为控件设置OnCheckedChangeListener监听器。





RadioButton与CheckBox有点类似,但与CheckBox不一样的地方:在界面上当RadioButton第一次被选中之后就不能再取消选中,但可以通过方法toggle()方法(该方法将单选按钮更改为与当前选中状态相反的状态)控制取消选中。

与CheckBox一样,我们可以通过setOnCheckedChangeListener来对单选按钮进行监听单选按钮选中/取消选中的状态变化。

radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
	public void onCheckedChanged(CompoundButton view,boolean isChecked){
		if(isChecked){
			//code here - CheckBox被选中
		}else{
			//code here - CheckBox取消选中
		}
	}
}

关于RadioGroup
RadioGroup继承于LinearLayout,是ViewGroup的孙类,用于创建一组按钮之间相互排斥的单选按钮组。在同一个单选按钮组中,勾选一个按钮则会取消该组中其它已经勾选的按钮的选中状态。


RadioButton与RadioGroup共同使用
1.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/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="奥迪"/>
    	<RadioButton android:id="@+id/radiobutton2" 
    	    android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="别克"/>
    	<RadioButton android:id="@+id/radiobutton3"
    		android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="大众"/>
    	<RadioButton
    		android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="宝马"/>
    </RadioGroup>
</LinearLayout>

2.在代码中:

radiogroup=(RadioGroup)findViewById(R.id.radiogroup);  
radio1=(RadioButton)findViewById(R.id.radiobutton1);  
radio2=(RadioButton)findViewById(R.id.radiobutton2);  
radio3=(RadioButton)findViewById(R.id.radiobutton3);  
radio4=(RadioButton)findViewById(R.id.radiobutton4);  
//设置RadioGroup的选中状态变化事件监听器
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
    @Override  
    public void onCheckedChanged(RadioGroup group, int checkedId) {  	//checkedId参数值为被选中的RadioButton的ID。
        if(checkedId==radio1.getId()){  
            //code here -- radiobutton1被选中
        } else if(checkedId==radio2.getId()){  
            //code here -- radiobutton2被选中
        } else if(checkedId==radio3.getId()){ 
            //code here -- radiobutton3被选中   
        } else {
            //code here -- radiobutton4被选中
        }
    }
});  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值