java - 即使在释放后如何使按钮保持选中状态?
问题描述
我试图让用户选择按钮后保持点击状态。我的程序是一个 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 个答案!
解决方案
如果允许在答案列表中检查单个答案,则需要使用RadioButtons
如果允许选中多个答案,则需要使用CheckBoxes或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>
并为背景选择创建一个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
}
}
});
推荐阅读
- php - AJAX 请求向控制器发送 null
- asciidoctor - 使用 Asciidoctor 处理等宽字体
- c++ - 如何使用 setw 和 setfill 重复字符输入?第一部分重复,但第二部分没有
- html - 如何在输入[类型文件]中制作图标(位置:绝对)clickabe?
- groovy - NoClassDefFoundError:Spock 2.0 中的 org.junit.rules.TestName:
- for-loop - Cicle FOR 没有 0
- python - 如何找到具有文本属性但在可单击但没有文本属性的主类中不可单击的类的元素
- windows - Git 将文件列为已更改但没有更改
- lua - 如何有效地提取大型主表中以指定字符串开头的所有值
- python - Python,变量未传入字符串