尝试这个:
final Button buttonA = (Button) findViewById(R.id.ButtonA);
final Button buttonB = (Button) findViewById(R.id.ButtonB);
buttonA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
buttonB.setBackgroundColor(Color.CYAN);
}
});
buttonB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
buttonA.setBackgroundColor(Color.RED);
}
});
或者
在 drawables 文件夹中创建 selector.xml 并在其中复制以下代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/focused_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/pressed" />
<item android:drawable="@drawable/normal" />
</selector>
在布局中复制 buttonA 的代码
<Button
android:id="@+id/buttonA"
style="@drawable/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button A" />