和按钮类似,这里采用cb1.setOnCheckedChangeListener(this);方法分别对3个CheckBox进行CheckChange事件绑定,然后在onCheckedChanged抽象函数中对点击CheckBox的状态进行获取并用Toast显示。

 //使用状态改变检查监听器
public class MainActivity extends Activity implements OnCheckedChangeListener {
private CheckBox cb1, cb2, cb3;//创建3个CheckBox对象 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//实例化3个CheckBox
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
} //重写监听器的抽象函数
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//buttonView 选中状态发生改变的那个按钮
//isChecked 表示按钮新的状态(true/false)
if (cb1 == buttonView || cb2 == buttonView || cb3 == buttonView) {
if (isChecked) {
//显示一个提示信息
toastDisplay(buttonView.getText() + "选中"); } else {
toastDisplay(buttonView.getText() + "取消选中");
}
}
} public void toastDisplay(String str) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}

了解上述用法之后,我们来看一下radioButton的用法:注意这里不同的地方是第13、14行,没有像CheckBox一样每一个控件绑定CheckChange监听器,而是将RadioGroup进行绑定。

 //使用状态改变监听器
public class MainActivity extends Activity implements OnCheckedChangeListener {
private RadioButton rb1, rb2, rb3;
private RadioGroup rg; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rb3 = (RadioButton) findViewById(R.id.rb3);
rg = (RadioGroup) findViewById(R.id.radGrp);
rg.setOnCheckedChangeListener(this);//将单选组绑定监听器
} //重写监听器函数
/**
* @param group:指单选组
* @param group:指单选组中发生状态改变的RadioButton的内存ID!
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (group == rg) {//因为当前程序中只有一个RadioGroup,此步可以不进行判定
String rbName = null;
if (checkedId == rb1.getId()) {
rbName = rb1.getText().toString();
} else if (checkedId == rb2.getId()) {
rbName = rb2.getText().toString();
} else if (checkedId == rb3.getId()) {
rbName = rb3.getText().toString();
}
Toast.makeText(this, "选择了下标为“" + rbName + "”的单选按钮",
Toast.LENGTH_LONG).show();
}
}
}

那这个RadioGroup是怎么和RadioButton结合在一起呈现多选一的效果的呢?我们来看看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"
>
<RadioGroup
android:id="@+id/radGrp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton1"
android:id="@+id/rb1"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton2"
android:id="@+id/rb2"
/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton3"
android:id="@+id/rb3"
/>
</RadioGroup>
</LinearLayout>

本文链接:http://www.cnblogs.com/zjutlitao/p/4229767.html

更多精彩:http://www.cnblogs.com/zjutlitao/

[安卓] 4、CheckBox、RadioButton和Toast简单用法的更多相关文章

  1. 安卓长按交互onCreateContextMenu的简单 用法

    1.可在activity和fragment中使用. 2.使用方法 (1)注册 registerForContextMenu(btn);//btn是要实现交互的控件 (2)重写onCreateConte ...

  2. WPF之Treeview控件简单用法

    TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...

  3. listActivity和ExpandableListActivity的简单用法

    http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...

  4. 【转】通知 Toast详细用法(显示view)

    原文网址:http://www.pocketdigi.com/20100904/87.html 今天学习Android通知 Toast的用法,Toast在手机屏幕上向用户显示一条信息,一段时间后信息会 ...

  5. android里Toast的用法

    在活动中,可以通过findViewById()方法获取到在布局文件中定义的元素,这里我们传入R.id.button_1,来得到按钮的实例,这个值是刚才在first_layout.xml中通过andro ...

  6. HttpURLConnection和HttpClient的简单用法

    HttpURLConnection的简单用法:先通过一个URL创建一个conn对象,然后就是可以设置get或者是post方法,接着用流来读取响应结果即可 String html = null; lon ...

  7. WebView的一些简单用法

    一直想写一个关于 WebView 控件的 一些简单运用,都没什么时间,这次也是挤出时间写的,里面的一些基础知识就等有时间再更新讲解一下,今天就先把项目出来做一些简单介绍,过多的内容可以看我的源码,都传 ...

  8. CATransition(os开发之画面切换) 的简单用法

    CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...

  9. jquery.validate.js 表单验证简单用法

    引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...

随机推荐

  1. file access , argc, argv[ ]

    _____main函数含有 两个参数 ,argc ,argv[] 这两个参数用以指示命令行输入的参数信息. argc 的值是输入的参数的数量.argv是一个数组,每个数组元素指向一个string字符串 ...

  2. html 高亮显示表格当前行

    html在线模拟网:http://www.w3school.com.cn/tiy/t.asp?f=html_basic 高亮显示表格当前行 <html> <head> < ...

  3. 项目Windows服务安装命令:

    sc create YY.SmsPlatform.RemoteDataCenter binPath= "E:\YY.SmsPlatform\YY.SmsPlatform.RemoteData ...

  4. linux各种命令

    命令  [选项]  [参数] read  -t  30    -p   "Please input a num: "   num 功能:将键盘输入的数赋予num ps   aux  ...

  5. jQuery中的事件和动画——《锋利的jQuery》(第2版)读书笔记2

    第4章 jQuery中的事件和动画 jQuery中的事件 加载DOM $(document).ready(function(){   // 编写代码... }); 可以简写成: $(function( ...

  6. 单片机TM4C123学习(七):I2C模块(温度传感器)

    I2C(Inter Intergrated Circuit)总线是Philips公司推出的一种用于IC器件之间连接的二线制串行扩展总线,它通过两根信号线(SDA-串行数据线:SCL-串行时钟线)在连接 ...

  7. servlet实现的三种方式对比(servlet 和GenericServlet和HttpServlet)

    第一种: 实现Servlet 接口 第二种: 继承GenericServlet 第三种 继承HttpServlet (开发中使用) 通过查看api文档发现他们三个(servlet 和GenericSe ...

  8. zabbix使用host metadata方式主动注册

    host metadata是zabbix2.2新增加的功能,该功能在zabbix-agent端可以自定义条件,在选择自动注册的时候,zabbix-server端可以根据host metadata来选择 ...

  9. python profile

    一.profile,cProfile 1. python -m cProfile myprogram.py python -m profile myprog.py2. 使用import profile ...

  10. 通过indexPath找到对应的cell

    在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 这个方法中通过 ...