首页 > 解决方案 > 即使在释放后如何使按钮保持选中状态?

问题描述

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

在问题之间切换时,我似乎无法找到一种方法来显示单击的按钮。

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 个答案!

标签: javaandroidandroid-layoutandroid-buttonandroid-togglebutton

解决方案


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

如果允许选中多个答案,则需要使用CheckBoxesToggleButtons

这是使用 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>

并为背景选择创建一个drawable

<?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>

爪哇:

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
        }
    }
});

在此处输入图像描述


推荐阅读