2014-10-16 325 views
1

我想要一个具有背景图片的按钮,当我点击该按钮时,按钮的背景或颜色发生变化(并且我们转到其他活动)。 告诉我怎么做到这一点。点击时更改按钮背景

回答

0

创建xml文件并将其保存在你的项目中drawable一个文件夹,让bg.xml,设置这个作为你Button

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
     <item android:drawable="@drawable/button_pressed" android:state_pressed="true" /> 
     <item android:drawable="@drawable/button_notpressed" /> 
</selector> 

其中button_pressedbutton_notpressed背景都是你想要的图片上显示的点击转换和button

在你活动的xml文件普通视图,设置button作为bg.xml的背景

<Button 
     android:id="@+id/btn" 
     android:layout_width="150dp" 
     android:layout_height="60dp" 
     android:textSize="20dp" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="450dp" 
     android:background="@drawable/bg" <--! set it like this--> 
     android:text="Submit" 
     android:textColor="#FFFFFF" /> 
+0

感谢ü非常 – Paul 2014-10-16 13:21:53

+0

你能解释一下我的选择(是什么,能这样做) – Paul 2014-10-16 13:25:37

+0

[请参见本(http://developer.android.com/reference/java/nio/channels/Selector.html ) – nobalG 2014-10-16 13:27:49