RadioGroup,RadioButton CheckBox控件的使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
  <RadioGroup 
      android:id="@+id/genderGroup"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      >
     <RadioButton 
         android:id="@+id/male"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="@string/male"
         />
     <RadioButton 
        android:id="@+id/female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/female"
                 />
  </RadioGroup>
  
  <CheckBox 
      android:id="@+id/swim"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/swim"
      />
  
   <CheckBox 
      android:id="@+id/run"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/run"
      />
   
    <CheckBox 
      android:id="@+id/read"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/read"
      />


</LinearLayout>

----------------------------------------------------------------------------------



package com.example.helloworld;



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.Toast;


public class RadioAndCheckBox extends Activity {


//定义变量
private RadioGroup genderGroup;
private RadioButton maleButton;
private RadioButton femaleButton;

private CheckBox swim;
private CheckBox run;
private CheckBox read;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radioandcheckbox);

//通过Id获取对象
genderGroup=(RadioGroup)findViewById(R.id.genderGroup);
maleButton=(RadioButton)findViewById(R.id.male);
femaleButton=(RadioButton)findViewById(R.id.female);

swim=(CheckBox)findViewById(R.id.swim);
run=(CheckBox)findViewById(R.id.run);
read=(CheckBox)findViewById(R.id.read);

//为RadioGroup对象添加监听的事件,该添加的监听事件与Button的事件不同
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
if(maleButton.getId()==checkedId){
System.out.println("选择了男生");
Toast.makeText(RadioAndCheckBox.this, "male", Toast.LENGTH_SHORT).show();
}else if(femaleButton.getId()==checkedId){
System.out.println("选择了女生");
Toast.makeText(RadioAndCheckBox.this, "female", Toast.LENGTH_SHORT).show();
}
}
});

swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton button, boolean checked) {
if(checked){
System.out.println("swim 被选择了");
}else{
System.out.println("swim 被撤销了");
}

}
});

run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton button, boolean checked) {
if(checked){
System.out.println("run 被选择了");
}else{
System.out.println("run 被撤销了");
}

}
});




read.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton button, boolean checked) {
if(checked){
System.out.println("read 被选择了");
}else{
System.out.println("read 被撤销了");
}

}
});







}




}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值