change image in button while is pressed
i have this code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/fundo" /> <!-- pressed -->
<item android:drawable="@drawable/fundo2" /> <!-- default -->
</selector>
in other file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn"
/>
</FrameLayout>
and in the class
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
return true;
case MotionEvent.ACTIO开发者_开发问答N_UP:
mp.pause();
return true;
}
return true;
}
i don't know why, when the button is pressed the image don't change, what is the reason?
thanks!!
Instead of android:background="@drawable/btn" please use android:src="@drawable/btn".
It Works for me .
Inside ur xml, for the button background give ur selector xml file name.
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ur state change xml file name"
/>
You can use setBackgroundResource(ResId) for the change on button u can write this in the OnClick() method of the Button.
精彩评论