您的位置:首页 > 移动开发 > Android开发

android中checkbox和radiobutton的使用

2011-03-15 11:30 218 查看
package com.example.checkButton;

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

public class checkButton extends Activity {
/** Called when the activity is first created. */
private RadioGroup genderGroup=null;
private RadioButton male=null;
private RadioButton female=null;
private CheckBox svim=null;
private CheckBox read=null;
private CheckBox run=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
male = (RadioButton)findViewById(R.id.male);
female = (RadioButton)findViewById(R.id.female);
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(male.getId() == checkedId)
{
System.out.println("male is checked");
}
else if(female.getId()==checkedId)
{
System.out.println("female is checked");
}
}
});

svim = (CheckBox)findViewById(R.id.svim);
read = (CheckBox)findViewById(R.id.read);
run = (CheckBox)findViewById(R.id.run);
svim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
// TODO Auto-generated method stub
if(ischecked)
{
System.out.println("svim is checked");
}
else
{
System.out.println("svim is unchecked");
}

}

}
);
run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
// TODO Auto-generated method stub
if(ischecked)
{
System.out.println("run is checked");
}
else
{
System.out.println("run is unchecked");
}

}

}
);
read.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
// TODO Auto-generated method stub
if(ischecked)
{
System.out.println("read is checked");
}
else
{
System.out.println("read is unchecked");
}

}

}
);

}
}
checkbutton.java中:

 

main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<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_width="wrap_content"
android:layout_height="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/svim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/svim"/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read"/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run"/>

</LinearLayout>


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息