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

1.CheckBox

布局文件:

<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: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" ><CheckBox android:id="@+id/eatId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="吃饭"/><CheckBox android:id="@+id/sleepId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="睡觉"/><CheckBox android: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:

public class MainActivity extends Activity
{private CheckBox eatBox;private CheckBox sleepBox;private CheckBox playBox;private CheckBox allBox;@Overrideprotected void onCreate(Bundle savedInstanceState){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);// OnBoxClickListener listener = new OnBoxClickListener();
//        OnBoxCheckedListener listener2 = new OnBoxCheckedListener();CheckedBoxListener listener3 = new CheckedBoxListener();// eatBox.setOnClickListener(listener);// sleepBox.setOnClickListener(listener);// playBox.setOnClickListener(listener);
eatBox.setOnCheckedChangeListener(listener3);sleepBox.setOnCheckedChangeListener(listener3);playBox.setOnCheckedChangeListener(listener3);allBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked){eatBox.setChecked(isChecked);sleepBox.setChecked(isChecked);playBox.setChecked(isChecked);}});}class CheckedBoxListener implements OnCheckedChangeListener{private int count = 0;@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked){if (isChecked){count++;if (count == 3){allBox.setChecked(isChecked);}}else{count--;allBox.setChecked(isChecked);}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);return true;}// CheckBox点击监听器class OnBoxClickListener implements OnClickListener{@Overridepublic void onClick(View view){CheckBox box = (CheckBox) view;if (box.getId() == R.id.eatId){System.out.println("eatBox");} else if (box.getId() == R.id.sleepId){System.out.println("sleepBox");} else if (box.getId() == R.id.playId){System.out.println("playBox");}if (box.isChecked()){System.out.println("Box is checked");} else{System.out.println("Box is unChecked");}System.out.println("CheckBox is clicked!");}}// CheckBox状态改变监听器class OnBoxCheckedListener implements OnCheckedChangeListener{@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked){CheckBox box = (CheckBox) buttonView;if (box.getId() == R.id.eatId){System.out.println("eatBox");} else if (box.getId() == R.id.sleepId){System.out.println("sleepBox");} else if (box.getId() == R.id.playId){System.out.println("playBox");}if (isChecked){System.out.println(box.getText() + " is checked!");} else{System.out.println(box.getText() + " is unchecked!");}}}}

2.RadioGroup和RadioButton

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

<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"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" ><RadioGroup android:id="@+id/radioGroupId1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButton android: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><RadioGroup android:id="@+id/raidoGroupId2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButton android:id="@+id/womenButtonId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="women"/><RadioButton android:id="@+id/manButtonId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="man"/></RadioGroup></LinearLayout>

MainActivity:

public class MainActivity extends Activity
{private RadioGroup radioGroup;private RadioButton femaleRadio;private RadioButton maleRadio;private RadioGroup radioGroup2;private RadioButton womenRadio;private RadioButton manRadio;@Overrideprotected void onCreate(Bundle savedInstanceState){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);RadioGroupListener listener = new RadioGroupListener();radioGroup.setOnCheckedChangeListener(listener);radioGroup2.setOnCheckedChangeListener(listener);}class RadioGroupListener implements OnCheckedChangeListener{@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId){if(checkedId == femaleRadio.getId() || checkedId == womenRadio.getId()){womenRadio.setChecked(true);femaleRadio.setChecked(true);System.out.println("femaleRadio is cheched!");}else if(checkedId == maleRadio.getId() || checkedId == manRadio.getId()){manRadio.setChecked(true);maleRadio.setChecked(true);System.out.println("maleRadio is checked!");}}}class RadioButtonListener implements android.widget.CompoundButton.OnCheckedChangeListener{@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked){if(isChecked){System.out.println("RadioButton is checked!");}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

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

         //  group表示当前选中的这一组的RadioGroup对象,checkId表示的是当前这组中选中的那个单选框的ID     public void onCheckedChanged(RadioGroup group, int checkedId){if(checkedId == femaleRadio.getId() || checkedId == womenRadio.getId()){womenRadio.setChecked(true);femaleRadio.setChecked(true);System.out.println("femaleRadio is cheched!");}else if(checkedId == maleRadio.getId() || checkedId == manRadio.getId()){manRadio.setChecked(true);maleRadio.setChecked(true);System.out.println("maleRadio is checked!");}}

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

        //  buttonView表示的就是当前调用这个方法的那个RadioButton对象,isChecked表示当前是否为选择     public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){if(isChecked){System.out.println("RadioButton is checked!");}}

Android UI系列-----CheckBox和RadioButton(1)相关推荐

  1. Android开发 CompoundButton CheckBox Switch RadioButton

    1.CompoundButton 抽象了各种复合按钮的一个抽象类,继承自Button类. 2.CheckBox 复选框 有默认的复选框,设置宽高文字内容就可以直接用. 也可以在drawable下新建一 ...

  2. android中radiogroup作用,Android View系列---RadioGroup与RadioButton

    RadioGroup与RadioButton配合实现一组数据的单选问题. 插播一条信息,在设置RadioButton的textColor的选中效果时,不能在drawable中创建想xml,得在res/ ...

  3. android radiobutton 监听事件,Android View系列---RadioGroup与RadioButton

    RadioGroup与RadioButton配合实现一组数据的单选问题. 插播一条信息,在设置RadioButton的textColor的选中效果时,不能在drawable中创建想xml,得在res/ ...

  4. Android UI系列-----ScrollView和HorizontalScrollView

    本篇随笔将讲解一下Android当中比较常用的两个布局容器--ScrollView和HorizontalScrollView,从字面意义上来看也是非常的简单的,ScrollView就是一个可以滚动的V ...

  5. Android UI系列之侧滑粘稠效果的实现

    转载自:http://www.jianshu.com/p/fb202d67c26b 之前在UI中国上看到一个侧滑效果,觉得还不错,于是就想实现一下. UI效果是这样的:http://www.ui.cn ...

  6. Android UI系列 - 布局 - 属性详解

    本文转自:http://www.cnblogs.com/chiao/archive/2011/08/25/2153652.html LinearLayout布局: 线性版面配置,在这个标签中,所有元件 ...

  7. Android零基础入门第20节:CheckBox和RadioButton使用大全

    原文:Android零基础入门第20节:CheckBox和RadioButton使用大全 本期先来学习Button的两个子控件,无论是单选还是复选,在实际开发中都是使用的较多的控件,相信通过本期的学习 ...

  8. Android面试系列文章2018之内存管理之UI卡顿篇

    Android面试系列文章2018之内存管理之UI卡顿篇 1.UI卡顿的原理   60ftp –> 16ms: Android系统每隔16ms都会对界面进行渲染一次,造成卡顿的原因就是Andro ...

  9. Android高级UI系列教程(二)

    上期回顾 Android高级UI系列教程(一)_我想月薪过万的博客-CSDN博客https://blog.csdn.net/qq_41885673/article/details/121870917 ...

最新文章

  1. java调用百度推送详解,关于百度推送,请教一下大家
  2. CentOS上安装skype
  3. Flask-SQLALchemy 连接数据库
  4. 名言为什么不能当论据_为什么您的工作场所论据没有您想要的那么有效
  5. javascript学习笔记(十五) 间歇调用和超时调用
  6. git:致命的:无法从远程存储库读取
  7. TensorFlow2.0学习使用笔记
  8. 【微信篇】微信自动登录设置
  9. libyuv的编译使用
  10. Appium自动化下拉刷新
  11. php实现微信第三方登录
  12. # UDIG配图(sld)
  13. uva 12307 - Smallest Enclosing Rectangle(旋转卡壳)
  14. 做一个企业网站需要多少钱?
  15. 74ls163是同步清零吗_74LS163是具有同步清零功能的十六进制计数器,要
  16. Linux登入Oracle数据库修改密码
  17. 引导最大内存_32位系统内存小的解决方法
  18. 认沽期权长短仓应用法
  19. VLAN应用篇系列:(7)华为 H3C交换机VLAN聚合功能(实现不同VLAN,同一网段,二层隔离,三层互通)
  20. 【资源总结】前端资源收集

热门文章

  1. FastJson 简单使用
  2. java正则表达式获取指定两个字符串之间内容
  3. 深入理解Java中为什么内部类可以访问外部类的成员
  4. SQL Server 对比数据库差异
  5. PHP解决方案@黑名单过滤
  6. 【十五分钟Talkshow】fmplan(十五分钟计划)的初步想法
  7. 备忘录吕吕没有备忘录十新建_一份备忘单,可帮助您记住CSS自定义属性
  8. graphql是什么_为什么GraphQL是避免技术债务的关键
  9. 让我们讨论一下变量,以及为什么要在JavaScript中使用它们。
  10. pix怎么抚养另一只猫_在工作和抚养两个孩子的同时,我如何在一年内获得第二学位并获得了5个开发人员认证...