单击时无法更改按钮背景颜色

Unable to change button background color on click

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

<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>

When I click the button the color changes and disappears I want it to stay until user press any other button

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

例如,您可以通过 Button 上的 setActivated() 切换激活状态。然后,在您的 btn_selector 资源中,您可以为 android:state_activated="true"android:state_activated="false".

使用不同的可绘制对象

您可以通过使用 ContextCompat.getDrawable() 方法设置可绘制对象以编程方式执行此操作:


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

}