RadioGroup、RadioButton、CheckBox、Toast用法
xml布局文件如下:
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/number2"
android:orientation="vertical">
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
</RadioGroup>
<CheckBox
android:id="@+id/swim"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sex"
android:text="游泳"/>
<CheckBox
android:id="@+id/football"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/swim"
android:text="足球"/>
MainActivity.java的OnCreate方法中相应的代码如下:
genderGroup = (RadioGroup)findViewById(R.id.sex);
femaleButton = (RadioButton)findViewById(R.id.female);
maleButton = (RadioButton)findViewById(R.id.male);
genderGroup.setOnCheckedChangeListener(new GenderGroupListener());
swimBox = (CheckBox)findViewById(R.id.swim);
footBallBox = (CheckBox)findViewById(R.id.football);
swimBox.setOnCheckedChangeListener(new HobbykBoxListener());
footBallBox.setOnCheckedChangeListener(new HobbykBoxListener());
定义genderGroup、CheckBox的监听器,注意二者的监听器的参数不同:
class GenderGroupListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//group点击的组的对象,checkedId组中的RadioButton对象的ID
if(femaleButton.getId() == checkedId){
Toast.makeText(MainActivity.this, "女", Toast.LENGTH_SHORT).show();
}
else if(maleButton.getId() == checkedId){
Toast.makeText(MainActivity.this, "男", Toast.LENGTH_SHORT).show();
}
}
}
class HobbykBoxListener implements android.widget.CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
// TODO Auto-generated method stub
//isChecked是否选中,如果选中则传入真,否则传入假
if(isChecked){
Toast.makeText(MainActivity.this, buttonView.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}
RadioGroup、RadioButton、CheckBox、Toast用法的更多相关文章
- android基本控件学习-----RadioButton&CheckBox
RadioButton(单选框)和CheckBox(复选框)讲解: 一.基本用法和事件处理 (1)RadioButton单选框,就是只能选择其中的一个,我们在使用的时候需要将RadioButton放到 ...
- 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
[源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...
- android单选按钮选择,RadioGroup,radioButton
android单选按钮选择,RadioGroup,radioButton 14. 四 / android基础 / 没有评论 单选布局绑定 如何识别选择
- Android RadioGroup/RadioButton
RadioGroup RadioButton的集合,提供多选一的机制 属性: android:orientation="horizontal/vertical&quo ...
- 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch
原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...
- JSTL select和checkbox的用法
select的 用法 <select id="roleIds" name="roleIds" multiple="true" back ...
- [Android]--RadioGroup+RadioButton实现底部导航栏
RadioGroup+RadioButton组合方式打造简单实用的底部导航栏 代码块: <?xml version="1.0" encoding="utf-8&qu ...
- [安卓] 4、CheckBox、RadioButton和Toast简单用法
和按钮类似,这里采用cb1.setOnCheckedChangeListener(this);方法分别对3个CheckBox进行CheckChange事件绑定,然后在onCheckedChange ...
- Android checkbox和radiobutton 以及Toast和AlertDialog的使用
package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import an ...
随机推荐
- cookies,sessionStorage 和 localStorage区别
cookies,sessionStorage 和 localStorage 的区别 cookie是网站为了标示用户身份而储存在用户本地终端(Client Side)上的数据(通常经过加密). cook ...
- POJ1860 Currency Exchange(bellman-ford)
链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...
- 使用Spring JdbcTemplate实现数据库操作
今天我来演示 关于JDBCTemplate实现对数据库的查询和添加 首先是添加 第一步大家都知道 创建一个实体类 然后写一个方法 把实体类当参数传进去 在实现这个接口 JdbcDaoSupport这个 ...
- C#_技巧:计算代码块运行的时间
System.Diagnostics下类Stopwatch,给程序代码块运行计时, 利用start()和stop()方法来标记代码快. 该命名空间下还有一些其他类,可以对程序进行诊断(diagnosi ...
- [Voice communications] 看得到的音频流
上文介绍了 Web Audio API 的相关知识,以及如何在你的 web 程序中引入 音频流,内容都是介绍性的,所以没有写太多 DEMO.本文重点讲解如何利用 Web Audio API 中的中间节 ...
- JWS.Mono如何进行“在线安装”
这里话就不多说了,使用方法如下: wget http://jhonge.net/down4load/1413998270361/jwsmono_net.sh chmod a+x jwsmono_net ...
- 神兵利器——Alfred
有人的地方就有江湖,有江湖就有纷争. 很多人说我的文字风格相对轻松和温和,那是因为我很早就认识到,我们没有教育脑残和喷子的义务.在网际多年,看过太多虚拟的刀锋和鲜血,很多人被彻头彻尾的粉碎,挫骨扬灰, ...
- jieba.NET与Lucene.Net的集成
首先声明:我对Lucene.Net并不熟悉,但搜索确实是分词的一个重要应用,所以这里还是尝试将两者集成起来,也许对你有一参考. 看到了两个中文分词与Lucene.Net的集成项目:Lucene.Net ...
- LinuxThreads 和 NPTL
http://www.ibm.com/developerworks/cn/linux/l-threading.html Linux 线程模型的比较:LinuxThreads 和 NPTL 进行移植的开 ...
- Docker学习笔记
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何 ...