RadioButton之互斥选择和Toast显示
前言:
RadioButton用来单选并且用Toast来进行提示所选内容
RadioButton标签单独写的时候不能出现互斥现象,代码如下
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="男"
android:id="@+id/nan"/>
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="女"
android:id="@+id/nv"/>
效果:

此时在屏幕中并不会互斥的选择,这是和我们意愿不符合,这是应该把这两个标签加在<RadioGroup><RadioGroup/>中,来实现可以互斥选择的效果
代码如下
<RadioGroup
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="男"
android:id="@+id/nan"/>
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="女"
android:id="@+id/nv"/>
</RadioGroup>
这样就可以实现互斥选择了
接下来实现当选择男是提示你选择男,
在RadioAcivity中的代码为
public class RadioActivity extends Activity {
private RadioButton nan, nv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.radio);
nan = (RadioButton) findViewById(R.id.nan);
nv = (RadioButton) findViewById(R.id.nv);
nan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stu
if (isChecked) {
Toast.makeText(RadioActivity.this,
"您选择的是:" + nan.getText(), 1).show();
}
}
});
nv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stu
if (isChecked) {
Toast.makeText(RadioActivity.this, "您选择的是:" + nv.getText(),
1).show();
}
}
});
}
}
我们知道RadioButton和CheckBox都为CompoundButton的子类,按钮有按钮的监听器,RadioButton也有自己的监听器,在初始化控件后,开始设置监听器(setCheckedChangeLintener),如12行和25行,我们使用匿名类来实现监听事件,因为onCheckedChangeListener是在CompoundButton下定义的,所以实例化onCheckedChangeListener对象,要加外部类名【此知识点是Java内部类知识点】,然后重写onCheckedChanged方法,这方法有两个参数一个是 Compound buttonView 一个是boolean isChecked当我们点过RadioButton时,isChecked为true
接下来就是用Toast来显示,注意第一个参数不是this 而是RadioActivity.this
RadioButton之互斥选择和Toast显示的更多相关文章
- 延长Toast显示时间
---恢复内容开始--- 由于Toast的显示时间只有两种: Toast.LENGTH_SHORT: 2秒 Toast.LENGTH_LONG: 3.5秒 而且是写死的,没给开发者自定义时间的权利,所 ...
- android 自定义Toast显示风格
1.创建一个自己想要显示Toast风格的XML如下代码(toast_xml.xml): <?xml version="1.0" encoding="utf-8&qu ...
- 设置Toast显示位置
设置Toast显示位置 两个方法可以设置显示位置: 方法一:setGravity(int gravity, int xOffset, int yOffset)三个参数分别表示(起点位置,水平向右位移, ...
- Android:剖析源码,随心所欲控制Toast显示
前言 Toast相信大家都不会陌生吧,如果对于Toast不甚了解,可以参考我的上一篇博客<Android:谈一谈安卓应用中的Toast情节>,里面有关于Toast基础比较详细的介绍.但是如 ...
- IE 选择文字后 显示小箭头 加速按钮
IE - 工具 - Internet选项 - 高级 - 不选择<在选择是显示加速按钮>选项就可以了.
- [Unity3D]Unity3D游戏开发之在3D场景中选择物体并显示轮廓效果
大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是blog.csdn.net/qinyuanpei. 在<仙剑奇侠传>.<古剑奇谭>等游戏中,常常须要玩家在一个3D场景中 ...
- 开发技巧01——改变Toast显示位置
1.获得Toast对象——Toast toast = Toast.makeText(this, "Top Left!", Toast.LENGTH_SHORT); 2.Toast对 ...
- ListView中RadioButton实现单项选择
1:FragmentHack5.java public class FragmentHack5 extends Fragment { View view; ListView lvCountries; ...
- 小米手机Toast显示带应用名称问题解决方法
近期为了适配刘海屏,向公司申购了一步小米8的手机,然后测试人员那边测出来一堆适配的问题,其中有一个每一个Toast会显示app的名称+显示的内容,然后网上查找了一下解决方法记录一下,顺便封装了Toas ...
随机推荐
- 《SQL 进阶教程》 查找局部不一致的数据
-- 从下面这张商品表里找出价格相等的商品的组合 select * from products p1LEFT JOIN products p2on p1.price = p2.price and p1 ...
- android开发如何在页面之间传参
第一个页面跳转 传递值 Button bn1=(Button)findViewById(R.id.btn_Login); //跳转bn1.setOnClickListener(new OnClickL ...
- MyBatis 配置文件详解
根元素<configuration>,子元素: <properties> <setttings> <typeAliases> <typeHandl ...
- 由Nginx反向代理引出的JCaptcha验证码验证失败的问题
搜索关键字: 1)Windows本地开发正常,部署到Linux远程服务器上JCaptcha验证失败 2)Linux远程服务器上JCpatcha验证失败 3)Nginx反向代理后JCaptcha验证失败 ...
- MySQL定时任务event,储存过程(定时删除指定时间前90天指定表的数据)
MySQL定时任务event,储存过程(定时删除指定时间前90天指定表的数据) 分类: MySql5.x2014-06-23 15:16 1266人阅读 评论(0) 收藏 举报 mysql数据库 &l ...
- eclipse 编辑窗口不见了(打开左边的java、xml文件,中间不会显示代码)
参考:https://blog.csdn.net/u012062810/article/details/46729779
- 第2节 storm实时看板案例:9、实时看板综合案例
=================================== 10.实时看板案例 10.1 项目需求梳理 根据订单mq,快速计算双11当天的订单量.销售金额.
- activity添加切换动画之后出现的黑色背景问题
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> & ...
- java程序员的就业指导(重点)
想要成为合格的Java程序员或工程师到底需要具备哪些专业技能,面试者在面试之前到底需要准备哪些东西呢?本文陈列的这些内容既可以作为个人简历中的内容,也可以作为面试的时候跟面试官聊的东西,你可以把这些内 ...
- git/github error: failed to push some refs to 'https://github.com/shenhaha/cloudletter.git'
git 提交代码到github上报如下错误 报错分析: 解决方法: 关闭这两个设置 再次提交代码 success