主要记录一下CheckBox多选框和RadioGroup、RadioButton单选框的设置以及注册监听器

1.CheckBox

布局文件:

<LinearLayoutxmlns: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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:orientation="vertical"tools:context=".MainActivity"><CheckBoxandroid:id="@+id/eatId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="吃饭"/><CheckBoxandroid:id="@+id/sleepId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="睡觉"/><CheckBoxandroid:id="@+id/playId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="玩游戏"/><CheckBoxandroid:id="@+id/allId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="全选"/></LinearLayout>

MainActivity:

publicclassMainActivityextendsActivity{privateCheckBoxeatBox;privateCheckBoxsleepBox;privateCheckBoxplayBox;privateCheckBoxallBox;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);eatBox=(CheckBox)findViewById(R.id.eatId);sleepBox=(CheckBox)findViewById(R.id.sleepId);playBox=(CheckBox)findViewById(R.id.playId);allBox=(CheckBox)findViewById(R.id.allId);//OnBoxClickListenerlistener=newOnBoxClickListener();//OnBoxCheckedListenerlistener2=newOnBoxCheckedListener();CheckedBoxListenerlistener3=newCheckedBoxListener();//eatBox.setOnClickListener(listener);//sleepBox.setOnClickListener(listener);//playBox.setOnClickListener(listener);eatBox.setOnCheckedChangeListener(listener3);sleepBox.setOnCheckedChangeListener(listener3);playBox.setOnCheckedChangeListener(listener3);allBox.setOnCheckedChangeListener(newOnCheckedChangeListener(){@OverridepublicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){eatBox.setChecked(isChecked);sleepBox.setChecked(isChecked);playBox.setChecked(isChecked);}});}classCheckedBoxListenerimplementsOnCheckedChangeListener{privateintcount=0;@OverridepublicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){if(isChecked){count++;if(count==3){allBox.setChecked(isChecked);}}else{count--;allBox.setChecked(isChecked);}}}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}//CheckBox点击监听器classOnBoxClickListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewview){CheckBoxbox=(CheckBox)view;if(box.getId()==R.id.eatId){System.out.println("eatBox");}elseif(box.getId()==R.id.sleepId){System.out.println("sleepBox");}elseif(box.getId()==R.id.playId){System.out.println("playBox");}if(box.isChecked()){System.out.println("Boxischecked");}else{System.out.println("BoxisunChecked");}System.out.println("CheckBoxisclicked!");}}//CheckBox状态改变监听器classOnBoxCheckedListenerimplementsOnCheckedChangeListener{@OverridepublicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){CheckBoxbox=(CheckBox)buttonView;if(box.getId()==R.id.eatId){System.out.println("eatBox");}elseif(box.getId()==R.id.sleepId){System.out.println("sleepBox");}elseif(box.getId()==R.id.playId){System.out.println("playBox");}if(isChecked){System.out.println(box.getText()+"ischecked!");}else{System.out.println(box.getText()+"isunchecked!");}}}}

2.RadioGroup和RadioButton

RadioGroup中可以放置多个RadioButton单选框,位于同一RadioGroup中的RadioButton每次只能选择一个

<LinearLayoutxmlns: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"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity"><RadioGroupandroid:id="@+id/radioGroupId1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/femailButtonId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"/><RadioButtonandroid:id="@+id/maleButtonId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"/></RadioGroup><RadioGroupandroid:id="@+id/raidoGroupId2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/womenButtonId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="women"/><RadioButtonandroid:id="@+id/manButtonId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="man"/></RadioGroup></LinearLayout>

MainActivity:

publicclassMainActivityextendsActivity{privateRadioGroupradioGroup;privateRadioButtonfemaleRadio;privateRadioButtonmaleRadio;privateRadioGroupradioGroup2;privateRadioButtonwomenRadio;privateRadioButtonmanRadio;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);radioGroup=(RadioGroup)findViewById(R.id.radioGroupId1);femaleRadio=(RadioButton)findViewById(R.id.femailButtonId);maleRadio=(RadioButton)findViewById(R.id.maleButtonId);radioGroup2=(RadioGroup)findViewById(R.id.raidoGroupId2);womenRadio=(RadioButton)findViewById(R.id.womenButtonId);manRadio=(RadioButton)findViewById(R.id.manButtonId);RadioGroupListenerlistener=newRadioGroupListener();radioGroup.setOnCheckedChangeListener(listener);radioGroup2.setOnCheckedChangeListener(listener);}classRadioGroupListenerimplementsOnCheckedChangeListener{@OverridepublicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){if(checkedId==femaleRadio.getId()||checkedId==womenRadio.getId()){womenRadio.setChecked(true);femaleRadio.setChecked(true);System.out.println("femaleRadioischeched!");}elseif(checkedId==maleRadio.getId()||checkedId==manRadio.getId()){manRadio.setChecked(true);maleRadio.setChecked(true);System.out.println("maleRadioischecked!");}}}classRadioButtonListenerimplementsandroid.widget.CompoundButton.OnCheckedChangeListener{@OverridepublicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){if(isChecked){System.out.println("RadioButtonischecked!");}}}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}}

注意:我们可以给RadioGroup注册一个OnCheckedChangeListener,引用的是android.widget.RadioGroup.OnCheckedChangeListener这个包下的监听器,其里面的方法是:

onCheckedChanged(RadioGroupgroup,(checkedId==femaleRadio.getId()||checkedId=="femaleRadioischeched!"(checkedId==maleRadio.getId()||checkedId=="maleRadioischecked!"

而我们还可以给每个RadioButton注册一个OnCheckedChangeListener,但是这里就要使用android.widget.CompoundButton.OnCheckedChangeListener 这个监听器类,其里面的方法:

"RadioButtonischecked!"

更多相关文章

  1. android xutils
  2. Android(安卓)简单封装一个精美、好用的菜单型PopupWindow
  3. Android(安卓)DynamicLoadApk 开源插件开发项目代码剖析
  4. Android中的HandlerThread和IntentService
  5. Android中常用的XML生成方法实例分析
  6. DDM dispatch reg wait timeout , Can't dispatch DDM chunk 解
  7. ConstraintLayout (约束布局)属性详情
  8. Android(安卓)EditText软键盘弹出时防止布局上移和关闭软键盘
  9. android 中ScrollView的使用

随机推荐

  1. Android中的Task和启动模式
  2. Android开发中如何监听指定URL地址的标签
  3. Android的致命问题——性能优化
  4. 【Android】画廊式的图片浏览器,使用Horiz
  5. Android(安卓)4.2 新特性体验
  6. java版 android遥控电脑关机
  7. Android开发之天气预报(四)UI界面实现
  8. Android笔面试
  9. Android参考书籍
  10. 聊聊 Android(安卓)中的三大框架