Android控件之——RadioGroup

Android控件之——RadioGroup、CheckBox

在界面程序开发中,常常使用到单选框和多选框,如:性别选择、单选题、多选题等。以下是Android中单选控件和多选控件的一种用法:


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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/radioGroup">
        
        <RadioButton 
            android:id="@+id/male"
            android:text="男"/>
        
        <RadioButton 
            android:id="@+id/female"
            android:text="女"/>
        
        <CheckBox 
            android:id="@+id/checkA"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="A.中国"/>
        
		<CheckBox 
		    android:id="@+id/checkB"
		    android:layout_width="fill_parent"
            android:layout_height="wrap_content"
		    android:text="B.美国"/>
			
		<CheckBox 
            android:id="@+id/checkC"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="C.德国"/>
        
		<CheckBox 
		    android:id="@+id/checkD"
		    android:layout_width="fill_parent"
            android:layout_height="wrap_content"
		    android:text="D.法国"/>
		
    </RadioGroup>

</LinearLayout>


Java代码如下:

package my.check.namespace;

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.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	
	private RadioButton male = null;
	private RadioButton female = null;
	private RadioGroup  group = null;
	
	private CheckBox checkA,checkB,checkC,checkD;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        male = (RadioButton)findViewById(R.id.male);
        female = (RadioButton)findViewById(R.id.female);
        group = (RadioGroup)findViewById(R.id.radioGroup);
        
        checkA = (CheckBox)findViewById(R.id.checkA);
        checkB = (CheckBox)findViewById(R.id.checkB);
        checkC = (CheckBox)findViewById(R.id.checkC);
        checkD = (CheckBox)findViewById(R.id.checkD);
        
        /*单选框监听器*/
        group.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(male.getId() == checkedId){
					dispToast("你选择:男");
				}else if(female.getId() == checkedId){
					dispToast("你选择:女");
				}
			}
        });
        
        /*多选框监听器*/
        checkA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				
				if(isChecked){
					dispToast("你选择A.中国");
				}else{
					dispToast("你取消了选择A");
				}
			}
        });
        
        /*多选框监听器*/
        checkB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				
				if(isChecked){
					dispToast("你选择B.美国");
				}else{
					dispToast("你取消了选择B");
				}
			}
        });
        
        /*多选框监听器*/
        checkC.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				
				if(isChecked){
					dispToast("你选择C.德国");
				}else{
					dispToast("你取消了选择C");
				}
			}
        });
        
        /*多选框监听器*/
        checkD.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				
				if(isChecked){
					dispToast("你选择D.法国");
				}else{
					dispToast("你取消了选择D");
				}
			}
        });
    }
    
    public void dispToast(String text){
    	
    	Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
    	toast.show();
    }
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值