android radiobutton和checkbox的使用

package com.studymar.radiocheckbox;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class RadioCheck extends Activity {
    //1.声明控件
    RadioGroup radioGroup=null;
    RadioButton radioButton1=null;
    RadioButton radioButton2=null;
    CheckBox checkBox1=null;
    CheckBox checkBox2=null;
    CheckBox checkBox3=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	//2.控件赋值
		setvalue();
	//3.控件监听
		setlistener();
	}
	private void setlistener() {
		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(radioButton1.getId()==checkedId){
					Log.i("123", "radioButton1 is checked");
				}else if(radioButton2.getId()==checkedId){
					Log.i("123", "radioButton2 is checked");
				}else{
					Log.i("123", "no radiobutton is checked");
				}	
			}
		});
		checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked==true){
					Log.i("123", "check1 is checked");
				}else{
					Log.i("123", "check1 is not checked");
				}	
			}
		});
		checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked==true){
					Log.i("123", "check2 is checked");
				}else{
					Log.i("123", "check2 is not checked");
				}	
			}
		});
		checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
	
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked==true){
					Log.i("123", "check3 is checked");
				}else{
					Log.i("123", "check3 is not checked");
				}	
			}
		});	
	}
	private void setvalue() {
		radioGroup=(RadioGroup)findViewById(R.id.rdgroup);
		radioButton1=(RadioButton)findViewById(R.id.radioone);
		radioButton2=(RadioButton)findViewById(R.id.radiotwo);
		checkBox1=(CheckBox)findViewById(R.id.checkone);
		checkBox2=(CheckBox)findViewById(R.id.checktwo);
		checkBox3=(CheckBox)findViewById(R.id.checkthree);		
	}

}
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >
  <RadioGroup 
      android:id="@+id/rdgroup"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      >
    <RadioButton 
        android:id="@+id/radioone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio1"
        android:layout_weight="1"
        /> 
    <RadioButton 
        android:id="@+id/radiotwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio2"
        android:layout_weight="1"
        /> 
  </RadioGroup>
  <CheckBox 
      android:id="@+id/checkone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/check1"
        
      /> 
   <CheckBox 
      android:id="@+id/checktwo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/check2"
        
      />  
   <CheckBox 
      android:id="@+id/checkthree"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/check3"
        
      />   

</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值