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

android radiobutton和checkbox的使用

2014-02-10 16:14 295 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: