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>
相关文章推荐
- Android开发历程_6(RadioButton和CheckBox的使用)
- Android控件之Button,RadioButton,CheckBox,DatePicker,DatePickerDialog的使用
- android radiobutton和checkbox的使用
- Android零基础入门第20节:CheckBox和RadioButton使用大全
- Android中RadioButton和Checkbox的使用
- Android选择控件,CheckBox和RadioButton使用大全
- Android零基础入门第20节:CheckBox和RadioButton使用大全
- Android零基础入门第20节:CheckBox和RadioButton使用大全
- Android开发视频教程之十二(RadioGroup、RadioButton、CheckBox和Toast的使用)
- 【按住你的心】——Android开发CheckBox&RadioButton控件的简单使用
- Android开发--RadioButton和CheckBox控件的使用
- 【按住你的心】——Android开发CheckBox&RadioButton控件的简单使用
- Android--CheckBox复选框、RadioGroup和RadioButton及其使用
- Android UI学习笔记 之RadioButton和CheckBox的使用
- Android checkbox和radiobutton 以及Toast和AlertDialog的使用
- Android开发--RadioButton和CheckBox控件的使用
- Android checkbox和radiobutton 以及Toast和AlertDialog的使用
- Android零基础入门第20节:CheckBox和RadioButton使用大全
- android RadioButton和CheckBox组件的使用方法
- android小功能:checkbox使用自己的背景点击切换背景