Android 是否可以取消选中复选框/单选按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11213699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:21:09  来源:igfitidea点击:

Is it possible to make a checkbox/radio button uncheckable?

android

提问by JuiCe

The program I am making has several views in which the user is trying to basically figure out a lot of boolean values. Sounds strange, but what I mean is that my device will connect to another device via bluetooth, and then read the status of that device. I basically display a checklist of the current status of that device. So depending on what that device is doing, different checkboxes are checked. Is there a way to make all of the checkboxes unselectable?

我正在制作的程序有几个视图,用户试图在这些视图中基本上找出很多布尔值。听起来很奇怪,但我的意思是我的设备将通过蓝牙连接到另一个设备,然后读取该设备的状态。我基本上显示了该设备当前状态的清单。因此,根据该设备正在执行的操作,会选中不同的复选框。有没有办法使所有复选框都不可选择?

Even better, is there a way to hardcode this into the XML file?

更好的是,有没有办法将其硬编码到 XML 文件中?

回答by nicholas.hauschild

Have you considered using android:clickablefor an xml attribute for your checkboxes?

您是否考虑过android:clickable为复选框使用xml 属性?

From the documentation:

从文档:

Defines whether this view reacts to click events.

定义此视图是否对单击事件作出反应。

If it cannot react to click events, then it effectively becomes uncheckable.

如果它不能对点击事件做出反应,那么它实际上变得不可检查。

回答by Woodsy

<CheckBox
        android:id="@+id/blahblah"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false" />

and in your code you can call cbox.setChecked(boolean)

在你的代码中你可以调用 cbox.setChecked(boolean)

回答by crocboy

Check this out: http://developer.android.com/reference/android/widget/CheckBox.html

看看这个:http: //developer.android.com/reference/android/widget/CheckBox.html

You can use checkbox.setEnabled(false)to make it unselectable.

您可以使用checkbox.setEnabled(false)使其无法选择。

Try this in your XML: android:enabled="false"

在你的 XML 中试试这个: android:enabled="false"

It's supposedly deprecated, but may work depending on your traget SDK.

它应该已被弃用,但可能会根据您的 traget SDK 工作。

Hope that helps!

希望有帮助!

回答by fasterAudi

to expand on what crocboy and nicholas.hauschild said.

扩展 crocboy 和 nicholas.hauschild 所说的内容。

Setting this in you XML

在你的 XML 中设置这个

    android:enabled="false" 

will make the button/checkbox be grayed-out, to show it is disabled

将使按钮/复选框变灰,以显示它已被禁用

Another way to do this is to set it to:

另一种方法是将其设置为:

    android:clickable="false"

Here is the code to make a RadioButton that is red, that turns green when checked (in your code), but can not be checked by the user.

这是使 RadioButton 为红色的代码,选中时(在您的代码中)变为绿色,但用户无法检查。

    android:checked="false"
    android:textColor="#d1d2d4"
    android:buttonTint="#d1202d"
    android:textColorHighlight="#248d51"
    android:clickable="false"

AND here is the code to check it (this is using JSON: so it is checking if english=="yes")

这是检查它的代码(这是使用 JSON:所以它正在检查 english=="yes")

    String yes = "yes";
    if(yourObject.english.equals(yes)) {
        if (m_english.isChecked()) {/*do nothing */}
        else m_english.toggle();
    }