ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-11-08 08:36:05  阅读:250  来源: 互联网

标签:android-button java android


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

如果允许检查多个答案,则需要使用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>

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

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

enter image description here

标签:android-button,java,android
来源: https://codeday.me/bug/20191108/2006742.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有