如何设置单选按钮与Android选中为默认RadioGroup中 [英] how to set radio button checked as default in radiogroup with Android

查看:1497
本文介绍了如何设置单选按钮与Android选中为默认RadioGroup中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建 RadioGroup中单选动态如下

RadioGroup radioGroup = new RadioGroup(context);
                    RadioButton radioBtn1 = new RadioButton(context);
                    RadioButton radioBtn2 = new RadioButton(context);
                    RadioButton radioBtn3 = new RadioButton(context);

                    radioBtn1.setText("Less");
                    radioBtn2.setText("Normal");
                    radioBtn3.setText("More");

                    radioBtn2.setChecked(true);

                    radioGroup.addView(radioBtn1);
                    radioGroup.addView(radioBtn2);
                    radioGroup.addView(radioBtn3);

下面的步骤 radioBtn2.setChecked(真); 引起 radioBtn2 必检项目。这意味着我无法取消 radioBtn2 通过检查其他两个单选按钮( radioBtn1 radioBtn3 )。我想作出这样的 RadioGroup中可以一次(现在,它可以检查两个单选一次)检查只有一个单选按钮。我该如何解决这个问题呢?

Here step radioBtn2.setChecked(true); causes radioBtn2 always checked. That means I cannot uncheck radioBtn2 by checking other two radio buttons (radioBtn1,radioBtn3). I want to make that RadioGroup can check only one radio button at a time (Now it can check two radiobutton at a time). How can I solve this problem?

推荐答案

您应该检查单选按钮在这样的RadioGroup中:

you should check the radiobutton in the radiogroup like this:

radiogroup.check(IdOfYou​​rButton)

当然,你首先需要一个ID为您的单选按钮

Of course you first have to set an Id to your radiobuttons

编辑:我忘了, radioButton.getId()作品以及,THX拉梅什

edit: i forgot, radioButton.getId() works as well, thx Ramesh

这篇关于如何设置单选按钮与Android选中为默认RadioGroup中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆