本文主要是介绍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 被撤销了");
}
}
});
}
}
这篇关于RadioGroup,RadioButton CheckBox控件的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!