android 怎么实现两组互斥的RadioGroup
来源:互联网 发布:itools不能下载软件 编辑:程序博客网 时间:2024/05/29 19:44
时间无多,大概说说就不上图了。
大家知道,RadioGroup里的所有RadioButton是互斥的,但是有时候想要实现两组RadioGroup,并且这两组也互斥。怎么做呢?
首先说错误的做法吧:
1.如果你的RadioButton是通过继承的方法来做的,并在继承RadioButton的MyRadioButton里重写isChecked方法,再进行逻辑判断来区别是哪组RadioGroup,那一定会产生让你抓狂的错误。我这么做了好多次,终于发现问题所在:你构造函数初始化的时候(执行super(content)时)会调用一次ischecked方法,你点击某一个RadioButton的时候会调用4次ischecked方法!你可以自己在这个方法里用log.v输出就明白了,具体它为什么这样执行我也不知道,希望高手解答。
2.如果你使用RadioGroup的setOnCheckedChangeListener监听器来进行判断,请注意,你在任何时候修改某个RadioButton的状态都会调用这个方法。所以,如果你在这个监听器里进行逻辑判断,然后想把之前选中的RadioButton给设置成不选中,它会之前RadioButton所属RadioGroup的监听器的方法,也会出现让人困惑的现象。
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { MyRadioButton now=(MyRadioButton)findViewById(checkedId); if(Checked_Id!=0) { MyRadioButton previous=(MyRadioButton)findViewById(Checked_Id); if(previous.type!=now.type) { switch(previous.type) { case My: try { mygroup.clearCheck(); }catch(Exception e){} break; case Server: try { servergroup.clearCheck(); }catch(Exception e){} break; } } } Checked_Id=checkedId; Checked_Name=now.getText().toString(); } });说明:我的MyRadioButton是继承自RadioButton的。里面有一个我用来区别属于不同RadioGroup的枚举变量,MyRadioButton.type,它有My和Server两种值。(有朋友可能会说,不能直接从RadioButton里调用某个方法返回它所属的RadioGroup吗?不好意思,我没找到,找到的话告诉我!)Cheked_Id保存之前选中的RadioButton的id。mygroup和servergroup分别是我的radiogroup
当发现之前RadioButton和当前的type不一样时,就调用RadioGroup.clearCheck()方法来清空该RadioGroup选中的。
刚学习,写得很烂,如果有好方法实现两组互斥的RadioGroup的朋友请告诉我!
- android 怎么实现两组互斥的RadioGroup
- android 实现两组互斥的RadioGroup--手记
- android在代码中怎么设置 RadioGroup的默认值
- android的RadioGroup讲解
- android RadioGroup的使用
- RadioGroup,Android的单选框。
- Android RadioGroup的监听
- Android.自定义控件的实现_RadioButton 和RadioGroup
- android中的ViewPager+Fragment+RadioGroup导航样式的实现
- android入门——用RadioGroup实现Fragment的切换
- Android 两个RadioGroup实现单选的效果
- Android中使用RadioGroup实现Fragment的切换
- RadioGroup - android自定义RadioGroup实现可以添加多种布局
- 基于RadioGroup的TabHost实现
- RadioGroup和RadioButton的实现
- android的tabhost+RadioGroup+PopupWindow
- Android RadioGroup控件的使用
- Android中RadioGroup的用法
- 九度OJ 1010 A+B
- 金山西山居2013程序挑战赛初赛 第三题
- Ubuntu 11.0下配置Hadoop
- 程序员谨防加班猝死之十大建议
- 借书方案知多少
- android 怎么实现两组互斥的RadioGroup
- WM程序出现ERROR: Cannot find dialog template with IDD 0x131的解决方法
- 批处理如何清除文本文档里面的重复行
- poj-openjudge 1041 For Short 解题报告
- FLEX 界面跳转
- 水仙花
- SecureCRT连接到Linux后使用sqlplus的退格删除
- 分解质因数
- poj-openjudge 1043:Bomb! 解题报告