主要记录一下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="玩游戏"/> <CheckBox
android: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; @Override
protected 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()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
eatBox.setChecked(isChecked);
sleepBox.setChecked(isChecked);
playBox.setChecked(isChecked);
}
});
} class CheckedBoxListener implements OnCheckedChangeListener
{
private int count = 0; @Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
if (isChecked)
{
count++;
if (count == 3)
{
allBox.setChecked(isChecked);
}
}
else
{
count--;
allBox.setChecked(isChecked);
}
}
} @Override
public 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
{
@Override
public 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
{
@Override
public 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="女"/> <RadioButton
android: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;
@Override
protected 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
{
@Override
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!");
}
}
} class RadioButtonListener implements android.widget.CompoundButton.OnCheckedChangeListener
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
if(isChecked)
{
System.out.println("RadioButton is checked!");
}
}
} @Override
public 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 UI系列-----时间、日期、Toasts和进度条Dialog

    您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...

  2. 【转】Android UI系列-----时间、日期、Toasts和进度条Dialog

    原文网址:http://www.cnblogs.com/xiaoluo501395377/p/3421727.html 您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注 ...

  3. Android中的checkbox和RadioButton的区别

    1.单个RadioButton在选中后,通过点击无法变为未选中       单个CheckBox在选中后,通过点击可以变为未选中 2.一组RadioButton,只能同时选中一个      一组Che ...

  4. Android UI系列-----RelativeLayout的相关属性

    本篇随笔将主要记录一些RelatieLayout的相关属性,并将猜拳游戏通过RelativeLayout实现出来 RelativeLayout的几组属性 第一组属性:android:layout_be ...

  5. Android UI系列-----LinearLayout的综合使用

    这里将会对LinearLayout的布局方式进行一个综合的使用,通过一个例子来看看LinearLayout的嵌套布局方式,在这之前首先介绍三个属性: 1.①android:layout_weigth: ...

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

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

  7. Android UI系列-----Dialog对话框

    您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...

  8. Android UI系列-----EditText和AutoCompleteTextView

    在这篇随笔里将主要讲解一下EditText和AutoCompleteTextView这个控件 1.EditText 首先我们先简单来说说EditText这个控件,这个就相当于我们平常web开发中的文本 ...

  9. Android UI系列--对话框(一)(AlertDialog,TimePickerDialog,DatePickerDialog,ProgressDialog)

    一.Dialog介绍 dialog就是一个在屏幕上弹出一个可以让用户做出一个选择,或者输入额外的信息的对话框,一个对话框并不会沾满我们整个的屏幕,并且通常用于模型事件当中需要用户做出一个决定后才会继续 ...

随机推荐

  1. python全栈开发day33-进程间的通信、进程间的数据共享,进程池

    一.昨日内容回顾: 1.  守护进程 1).p.saemon, 2 ).p.terminate 3 ).p.join 2.  同步控制 1).锁,Lock 互斥锁,解决数据安全.进程之间资源抢占问题. ...

  2. 2.Django|简介与静态文件| URL控制器

    1.简介  MVC Web服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的.松耦合的方式连接在一起,模型负责业务对象与数 ...

  3. Burp Suite之Intruder模块(四)

    Burp Suite之Intruder模块(三) Intruder介绍: Burp intruder是一个强大的工具,用于自动对Web应用程序自定义的攻击.它可以用来自动执行所有类型的任务您的测试过程 ...

  4. AngularJS之双向数据绑定,class绑定

    之前一直都是用vue来完成一些日常开发,初入AngularJS,记录一些日常开发遇到的问题. 1.双向数据绑定 AngularJS与vue的区别在于,vue采用的是虚拟DOM,模板文件上绑定的一大堆指 ...

  5. Java基础--对象的克隆

    文章转载自https://www.cnblogs.com/Qian123/p/5710533.html 阅读目录 为什么要克隆? 如何实现克隆 浅克隆和深克隆 解决多层克隆问题 总结 假如说你想复制一 ...

  6. JavaScript学习总结(六)——前端模块化开发

    早期的javascript版本没有块级作用域.没有类.没有包.也没有模块,这样会带来一些问题,如复用.依赖.冲突.代码组织混乱等,随着前端的膨胀,模块化显得非常迫切. 前端模块化规范如下: 一.前端模 ...

  7. 9,EasyNetQ-版本化消息

    要启用对版本化消息的支持,您需要确保配置所需的组件. 最简单的方法是: var bus = RabbitHutch.CreateBus( "host=localhost", ser ...

  8. bzoj 3450 期望分数

    自己只能想到O(n^2)的: dp[i][j] 表示 以i结尾,长度为j的o串的概率,然后在每次遇到x的时候算分数. 正解是: dp[i]表示前i个的答案,d[i]表示以i结尾的期望长度. 推的时候它 ...

  9. vi 命令 行首、行尾

    vim 跳到行首 : 数字 0 vim跳到行位 : $  [Shift + 4]

  10. javaweb中为mysql的curd多个值的语句

    更新语句 String sql = "update student set num=?,name=?,birthday=?,score=?,password=? where id=?&quo ...