【问题标题】:Unable to change button background color on click单击时无法更改按钮背景颜色
【发布时间】:2021-08-02 04:53:02
【问题描述】:

我知道这个主题有很多答案我已经尝试了所有可能的解决方案但无法解决问题。我有六个按钮,单击按钮时我想更改按钮的背景颜色、边框和文本颜色。当我单击按钮时,颜色会改变并消失,我希望它一直存在,直到用户按下任何其他按钮。请检查我的代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

        <androidx.appcompat.widget.AppCompatButton
             android:id="@+id/btn1"
             android:layout_width="50dp"
             android:layout_height="30dp"
             android:gravity="center"
             android:text="Button 1"
             android:background="@drawable/btn_selector"
             android:textColor="@color/text_color_selector"
             android:textSize="12sp" />
</LinearLayout>

btn_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_selected" android:state_pressed="true" />
    <item android:drawable="@drawable/btn_selected" android:state_focused="true" />
    <item android:drawable="@drawable/btn_unselected" android:state_pressed="false" />
    <item android:drawable="@drawable/btn_unselected" android:state_selected="false" />
</selector>

btn_selected.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
     <solid android:color="@android:color/white" />
     <stroke android:width="0.5dp" android:color="#4484F4" />
     <corners android:radius="100dip" />
</shape>

btn_unselected.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
     <solid android:color="@color/colaba_grey_fill" />
     <stroke android:width="0.5dp" android:color="#4484F4" />
     <corners android:radius="100dip" />
</shape>

【问题讨论】:

    标签: android button


    【解决方案1】:

    当我点击按钮时,颜色会改变并消失,我希望它一直存在,直到用户按下任何其他按钮

    您没有执行此操作的代码。 Button 没有在用户点击它时改变的持久状态。你必须自己做一些事情,用 Java/Kotlin 代码来做。

    例如,您可以通过Button 上的setActivated() 切换激活状态。然后,在 btn_selector 资源中,android:state_activated="true" 的可绘制对象与 android:state_activated="false" 的可绘制对象不同。

    【讨论】:

      【解决方案2】:

      您可以通过使用ContextCompat.getDrawable() 方法设置drawable 以编程方式完成此操作:

      
      public class MainActivity extends AppCompatActivity {
      
          private Drawable mDefaultButtonColor;
          private Drawable mSelectedButtonColor;
          private Button buttons[];
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              // Adding all buttons in an array
              buttons = new Button[]{
                      findViewById(R.id.btn1),
                      findViewById(R.id.btn2),
                      findViewById(R.id.btn3),
                      findViewById(R.id.btn4)
              };
          }
      
          // Method to set the drawable
          private void toggleButton(Button button, boolean isSelected) {
              button.setBackground(isSelected ?
                      ContextCompat.getDrawable(this, R.drawable.btn_selected)
                      : ContextCompat.getDrawable(this, R.drawable.btn_unselected));
          }
      
          // Callback for all the buttons using `android:onClick` XML attribute
          public void onButtonClick(View view) {
              for (Button button : buttons)
                  toggleButton(button, false);
              toggleButton((Button) view, true);
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2014-10-09
        • 2017-08-02
        • 2014-10-10
        • 1970-01-01
        • 1970-01-01
        • 2019-04-14
        • 2014-10-07
        • 2015-03-29
        相关资源
        最近更新 更多