[安卓] 4、CheckBox、RadioButton和Toast简单用法

和按钮类似,这里采用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简单用法的更多相关文章
- 安卓长按交互onCreateContextMenu的简单 用法
1.可在activity和fragment中使用. 2.使用方法 (1)注册 registerForContextMenu(btn);//btn是要实现交互的控件 (2)重写onCreateConte ...
- WPF之Treeview控件简单用法
TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...
- listActivity和ExpandableListActivity的简单用法
http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...
- 【转】通知 Toast详细用法(显示view)
原文网址:http://www.pocketdigi.com/20100904/87.html 今天学习Android通知 Toast的用法,Toast在手机屏幕上向用户显示一条信息,一段时间后信息会 ...
- android里Toast的用法
在活动中,可以通过findViewById()方法获取到在布局文件中定义的元素,这里我们传入R.id.button_1,来得到按钮的实例,这个值是刚才在first_layout.xml中通过andro ...
- HttpURLConnection和HttpClient的简单用法
HttpURLConnection的简单用法:先通过一个URL创建一个conn对象,然后就是可以设置get或者是post方法,接着用流来读取响应结果即可 String html = null; lon ...
- WebView的一些简单用法
一直想写一个关于 WebView 控件的 一些简单运用,都没什么时间,这次也是挤出时间写的,里面的一些基础知识就等有时间再更新讲解一下,今天就先把项目出来做一些简单介绍,过多的内容可以看我的源码,都传 ...
- CATransition(os开发之画面切换) 的简单用法
CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...
- jquery.validate.js 表单验证简单用法
引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...
随机推荐
- avalon2学习教程12数据验证
avalon2砍掉了不少功能(如ms-include,ms-data),腾出空间加了其他更有用的功能.数据验证就是其中之一.现在avalon2内置的验证指令是参考之前的oniui验证框架与jquery ...
- 详解Oracle DELETE和TRUNCATE 的区别(摘)
语法delete from aa truncate table aa 区别 1.delete from后面可以写条件,truncate不可以. 2.delete from记录是一条条删的,所删除的每行 ...
- node中的可读流和可写流
javascript的一个不足之处是不能处理二进制数据,于是node中引入了Buffer类型.这个类型以一个字节(即8位)为单位,给数据分配存储空间.它的使用类似于Array,但是与Array又有不同 ...
- Android-Junit-Report测试报告生成——Android自动化测试学习历程
视频地址: http://www.chuanke.com/v1983382-135467-384869.html 这个内容其实已经在用了,我在上一篇文章robotium—只有apk文件的测试中已经讲过 ...
- 第一次正式小用Redis存储
由于要做一个同一个页面上多种图表数据的下载,考虑到Azure上面的session很不稳定(可用Redis provider存储session,较稳定),故决定改为Azure支持的Redis,顺便也学习 ...
- window的git extensions保存密码
git extensions每次pull与push均要输入密码,为解决该问题,执行以下操作: win+r,在运行中输入:%USERPROFILE% 找到其中的.gitconfig文件,找到[crede ...
- Java写操作
//:ThinkingInJava/net.mindview.io/write2File.java package net.mindview.io; import java.io.BufferedRe ...
- DateTools时间插件
import java.text.DateFormat;import java.text.ParsePosition;import java.text.SimpleDateFormat;import ...
- 解决: Can’t connect to local MySQL server through socket /var/lib/mysql/mysql.sock
今天在搬瓦工上使用mysql 命令行,总报一个这样的错:Can't connect to local MySQL server through socket '/xxxxx/mysql.sock',一 ...
- AngularJs自定义指令详解(3) - scope
我们之所以要定义指令,目的是重用指令.假设有这么一个应用场景:在同一个html里使用了两次my-directive,第一个my-directive要展示的是Hello World,第二个my-dire ...