【问题标题】:How to make a button remain selected even after release?即使在释放后如何使按钮保持选中状态?
【发布时间】:2019-06-27 00:31:01
【问题描述】:

我试图让用户选择按钮后保持点击状态。我的程序是一个 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 个答案!

【问题讨论】:

  • 是选择题吗?
  • @M.Zain 是的,我忘了说!
  • 请检查答案,希望对您有所帮助

标签: java android android-layout android-button android-togglebutton


【解决方案1】:

听起来您应该在RadioGroup 中使用RadioButtons。

更多信息请参见the guide in the documentation

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

【讨论】:

    【解决方案2】:

    使用 RadioGroup。并用 ToggleButton 填充它(看起来最接近常规按钮)。 Another answer which shows how to set this up 然后您可以使用“radioGroup.getCheckedRadioButtonId();”检索左键单击的 ToggleButton 的 Id How to get checked option

    【讨论】:

      【解决方案3】:

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

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

      这是一个使用 ToggleButtons 模拟按钮被点击的 sn-p

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        • 2016-01-03
        相关资源
        最近更新 更多