【问题标题】:Radio Button setchecked cannot be set to true in Android单选按钮 setchecked 不能在 Android 中设置为 true
【发布时间】:2014-11-13 17:16:28
【问题描述】:

我正在尝试使用 setChecked() 进行单选按钮切换。

setChecked(false) 工作正常,但 setChecked(true) 不起作用。

还尝试了toggle(),即使这并没有取消选中单选按钮。

我需要一个不使用无线电组的解决方案。只需一个单选按钮,点击即可切换其状态。

MainActivity.java

public void radioCheck(View v) {
    System.out.println("radioCheck");
    RadioButton rb = (RadioButton) findViewById(R.id.radioButton1);

//rb.toggle();

    if (rb.isChecked() == true) {
        rb.setChecked(false);
    }
    else {
        rb.setChecked(true);
    }
}

activity_main.xml

<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_centerHorizontal="true"
    android:onClick="radioCheck"
    android:text="Set me" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/radioButton1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="39dp"
    android:onClick="radioCheck"
    android:text="On/ Off" />

令人惊讶的是,setChecked 在使用按钮取消选中单选按钮时有效,但在单选按钮上设置时同样无效。

【问题讨论】:

  • 尝试替换这段代码:if (rb.isChecked()){

标签: java android checkbox radio-button


【解决方案1】:

您可能会导致无限循环和堆栈溢出。 setChecked 完全没有必要,你不需要自己切换它。 onClick 用于对新选择做出反应,而不是自己选中/取消选中它。

来自http://developer.android.com/guide/topics/ui/controls/radiobutton.html

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton android:id="@+id/radio_pirates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pirates"
        android:onClick="onRadioButtonClicked"/>
    <RadioButton android:id="@+id/radio_ninjas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ninjas"
        android:onClick="onRadioButtonClicked"/>
</RadioGroup>

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_pirates:
            if (checked)
                // Pirates are the best
            break;
        case R.id.radio_ninjas:
            if (checked)
                // Ninjas rule
            break;
    }
}

【讨论】:

    【解决方案2】:

    试试这个方法,希望能帮助你解决问题。

    如果条件工作为真或假,那么当你得到 isChecked() 时已经返回真或假,所以不需要检查 (== true) 你在代码中做错的事情。

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">
    
        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set me" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="radioCheck"
            android:layout_marginTop="10dp"
            android:text="On/ Off" />
    
    </LinearLayout>
    

    MainActivity.java

    public class MainActivity extends Activity {
    
        private RadioButton radioButton1;
        private Button button2;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
            button2 = (RadioButton) findViewById(R.id.button2);
        }
    
        public void radioCheck(View v) {
            if (radioButton1.isChecked()) {
                radioButton1.setChecked(false);
            }
            else {
                radioButton1.setChecked(true);
            }
        }
    
    }
    

    【讨论】:

    • 但是 radioCheck 方法是在单击按钮时调用的,而不是 RadioButton
    • 或者只是:rb.setChecked(!((RadioButton)v).isChecked());
    • @HareshChhelana 这在单选按钮单击时不起作用(同样的旧问题仍然存在)。使用按钮时,应用程序崩溃返回按钮停止响应。
    • 谢谢,找到了解决这个问题的办法。单选按钮的基本功能是至少应该选择其中之一。就我而言,我只有一个单选按钮,这就是无法取消选择的原因。此功能只能使用复选框来实现。
    猜你喜欢
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 2021-11-13
    • 2019-05-08
    • 2022-01-01
    • 1970-01-01
    • 2011-05-07
    相关资源
    最近更新 更多