[安卓] 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 " ...
随机推荐
- {Reship}{Matting}Image Matting
======================================== http://www.alphamatting.com/index.html ==================== ...
- JDK 环境变量设置
.net转JAVA了.记心不好,记录一下. 安装好jdk(64位)后找到我的电脑(右键)>属性>高级选项卡>环境变量>,里面有管理员的用户变量,有系统变量.我选的是系统变量.点 ...
- codeforces731C Socks
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- MongoDB中的连接池
参见 http://www.cnblogs.com/huangfox/archive/2012/04/01/2428947.html
- 如何扫描二维码下载APK
将apk文件放到网站上,即用户可以通过www.xxx.com.cn/abc.apk直接下载 再www.xxx.com.cn/abc.apk这个字符串做成二维码就可以了. 问题: 直接放到网站后,输入下 ...
- Spring 依赖注入的方式
Spring 支持3中依赖注入的方式 1.属性注入 通过setter 方法注入Bean的属性或依赖的对象. <bean id = " " class = " &q ...
- Nginx配置配置文件nginx.conf的设置
引用自:http://www.ha97.com/5194.html #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_pro ...
- C#中TreeView与数据库绑定
protected void CreateTreeView() { TreeNode rootNode = new TreeNode(); rootNode.Text = "全部" ...
- 对CLR基本原理概念&垃圾回收机制的简单理解
前言,之前有说过C语言的函数&变量的一些基本概念,说得可能不是很好,先也把C#的.里相关的也说下,已成一统. 而说函数变量,其实主要就是GC,而GC又是CLR的主要内容,故就有了此文. CLR ...
- git 常用技巧
撤销修改 撤销本地修改 git reset --hard 或者 git checkout -- . 切换分支 切换到前一分支 git checkout - 切换到某个分支 git checkout & ...