我正在为平板电脑编写一个应用程序,在这种情况下,我连续有8个按钮,并且带有一些背景。现在,我想更改单击的唯一按钮以及所有8个按钮的背景。
我尝试制作可绘制的XML并处理按钮状态(如聚焦,按下),还尝试检查以防万一。但这仅会改变背景直到我按下它。这不是我的要求。因此,要实现这一点,我必须写出行数。
myDrawable
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/background_white_with_black_border" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/background_white_with_black_border" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/background_all_corners_five_dp_solid_login_btn_blue" />
<item android:drawable="@drawable/background_white_with_black_stroke" />
</selector>```
setting the above as buttons background didnt gave me result. So did this:
```button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button1.setBackground(getResources().getDrawable(R.drawable.background_all_corners_five_dp_solid_login_btn_blue));
baySelected = "Bay1";
button2.setBackground(getResources().getDrawable(R.drawable.background_white_with_black_stroke));
button3.setBackground(getResources().getDrawable(R.drawable.background_white_with_black_stroke));
button4.setBackground(getResources().getDrawable(R.drawable.background_white_with_black_stroke));
button5.setBackground(getResources().getDrawable(R.drawable.background_white_with_black_stroke));
button6.setBackground(getResources().getDrawable(R.drawable.background_white_with_black_stroke));
button7.setBackground(getResources().getDrawable(R.drawable.background_white_with_black_stroke));
button8.setBackground(getResources().getDrawable(R.drawable.background_white_with_black_stroke));```