我想在按下、聚焦和单击时更改按钮颜色,但我想我一定做错了什么,我的代码不起作用。有人请指教吗?谢谢!
这是我soundbtn.xml
的可绘制对象:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/sound_onpress_mdp" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/sound_onpress_mdp" /> <!-- focused -->
<item android:drawable="@drawable/sound" /> <!-- default -->
</selector>
然后我ImageButton
在布局中添加了背景:
<ImageButton
android:id="@+id/imageSound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/soundbtn"
android:contentDescription="@string/image_desc"
android:src="@drawable/sound" />
这是我的 Java 代码:
private ImageButton btnSound;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
btnSound =(ImageButton) findViewById(R.id.imageSound);
btnSound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnSound.setBackgroundResource(R.drawable.soundbtn);
}
});
}