1
我想改变按钮的颜色时按下,聚焦和点击,但我认为我一定做错了什么,我的代码不起作用。有人请咨询?谢谢!ImageButton颜色不变onPressed(),onFocus()
这是我在绘制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);
}
});
}
谢谢,我已经删除了它,它现在正在工作。 – Teresa 2013-03-08 11:06:15