如何在 Android 中设置单选按钮 [英] How to set a radio button in Android

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

问题描述

我有一个使用单选按钮的应用.此按钮的默认设置在 main.xml 文件中,即:

I have an app that uses radio buttons. The default for this button is set in the main.xml file, ie:

android:id="@+id/rb_sat1E"
android:checked="true"

在我拥有的 Java 文件中:

In the Java file I have:

final RadioButton radio1 = (RadioButton)findViewById(R.id.rb_sat1E);

我还在 Java 主文件中创建了一个重置"按钮,可以使用以下代码重置 TextView 信息,即.

I have also created a 'Reset' button in the main Java file and can use the following code to reset TextView information ie.

pos1_deg.setText("0.0");

但是如何重置单选按钮?我本来以为是这样的

But how do I reset a radio button? I would have thought it to be something like

radio1.setBoolean("TRUE");

但这根本不起作用.

非常感谢任何帮助.谢谢.

Any help greatly appreciated. Thanks.

推荐答案

供radioButton使用

For radioButton use

radio1.setChecked(true);

只有一个 RadioButton 是没有意义的.如果您有更多,则需要通过

It does not make sense to have just one RadioButton. If you have more of them you need to uncheck others through

radio2.setChecked(false); ...

如果您的设置只是开/关,请使用 CheckBox.

If your setting is just on/off use CheckBox.

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

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