如何使按钮在释放后仍保持选中状态?

How to make a button remain selected even after release?

我试图让按钮在用户选择后保持点击状态。我的程序是一个包含 10 个问题的测验,如果用户选择一个选项,然后 return 回答那个问题,它应该显示为已选择。

我似乎想不出一种方法来显示在问题之间切换时单击的按钮。

mAButton.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
            Log.i(TAG, "A Button Clicked");
            answers[questionIndex] = "Question: " + questionIndex + ", Answer: A";
            Toast t;
            t = Toast.makeText(MainActivity.this, R.string.recorded_toast, Toast.LENGTH_SHORT);
            t.show();
        }
});

这是我对其中一个答案的 onClick 示例(我有 5 个按钮用于答案)。我想要这样,如果用户选择了这个答案,按钮将一直被点击,直到他们选择不同的答案。我已经尝试了一些我在网上看到的东西,但到目前为止没有任何效果。

编辑:每个问题都是选择题,有 5 个答案!

听起来你应该使用 RadioButtons inside of a RadioGroup

有关更多信息,请参阅 the guide in the documentation

如果这不能帮助您解决问题,请clarify your question

使用广播组。并用 ToggleButton 填充它(看起来最接近常规按钮)。 Another answer which shows how to set this up Then you can retrieve the Id of the ToggleButton that was left clicked using "radioGroup.getCheckedRadioButtonId();" How to get checked option

如果允许在答案列表中勾选单个答案,则需要使用RadioButtons

如果允许勾选多个答案,则需要使用CheckBoxes or ToggleButtons

这是使用 ToggleButtons 的片段,它模拟按钮被点击

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />
</LinearLayout>

并为背景选择创建可绘制对象

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/checked_button" android:state_checked="true" />
    <item android:drawable="@color/unchecked_button" android:state_checked="false" />
</selector>

颜色

<resources>
    <?xml version="1.0" encoding="utf-8"?>
    ...
    <color name="checked_button">#989898</color>
    <color name="unchecked_button">#f8f8f8</color>

</resources>

Java:

ToggleButton button1 = findViewById(R.id.btn1);
ToggleButton button2 = findViewById(R.id.btn2);
ToggleButton button3 = findViewById(R.id.btn3);
ToggleButton button4 = findViewById(R.id.btn4);

button1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // Button1 is checked
        } else {
            // Button1 is unchecked
        }
    }
});