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

释放后如何使按钮保持选中状态?,第1张

概述我试图在用户选择按钮后使其保持点击状态.我的程序是一个10个问题的测验,如果用户选择一个选项然后返回该问题,则应按选择显示该问题.在问题之间切换时,我似乎无法找出一种将按钮显示为单击状态的方法.mAButton.setOnClickListener(newView.OnClickListener(){@Override

我试图在用户选择按钮后使其保持点击状态.我的程序是一个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>

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

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

总结

以上是内存溢出为你收集整理的释放后如何使按钮保持选中状态?全部内容,希望文章能够帮你解决释放后如何使按钮保持选中状态?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1081827.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存