简体   繁体   English

按下时更改按钮背景

[英]Change button background when pressed

I want to change background of a button when it is clicked. 我想更改单击按钮时的背景。 I tried to use a selector. 我试图使用选择器。 But It didn't work. 但这没有用。 Here is the selector (add_grp_slctr.xml): 这是选择器(add_grp_slctr.xml):

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

And the button : 和按钮:

       <Button
            android:id="@+id/addGrpBtn"
            android:layout_width="55dp"
            android:layout_height="45dp"
            android:layout_gravity="center"
            android:background="@drawable/add_grp_slctr"
            android:onClick="addGrpDialogOpen" />

add_grp_d and add_grp are images(png). add_grp_d和add_grp是images(png)。

I tried a similar code which will be white by default, black when pressed on an onclick of a button: 我尝试了类似的代码,默认情况下将为白色,当按下按钮的onclick时为黑色:

// * * *This is the btn_selector which is to be declared in drawable folder*** // * * *这是将在可绘制文件夹中声明的btn_selector ***

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@android:color/black" /> <!-- pressed -->
    <item android:drawable="@android:color/white" /> <!-- default -->
</selector>

and called this on the button.xml --> 并在button.xml上将其称为->

 android:background="@drawable/btn_selector"

Hope this would help..:) 希望这会有所帮助..::)

go through the http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList once. 一次浏览http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList Also state_focussed for button only works when you are focussing the button using a hardware-keyboard. 同样,仅当您使用硬件键盘为按钮聚焦时,state_focussed for button才起作用。 As for your case 至于你的情况

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

Use your selector 使用您的选择器

change your code like this 像这样改变你的代码

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

My Code 我的密码

<item android:drawable="@drawable/shadow_design_click" android:state_pressed="true"/>
<item android:drawable="@drawable/shadow_design" android:state_focused="true"/>
<item android:drawable="@drawable/shadow_design"/>

</selector>

I think you should change your selector a bit. 我认为您应该稍微更改选择器。 Check this answer here . 在此处检查此答案。

Instead of passing cuastom XML file, if you just want to change the color of you button then you can try with the following way. 如果您只想更改按钮的颜色,而不是传递cuastom XML文件,则可以尝试以下方式。

Button lineColorCode = (Button)findViewById(R.id.button1);

Now inside button's click event use following code. 现在,在按钮的click事件中使用以下代码。

int color = Color.parseColor("#AE6118"); //The color u want             
lineColorCode.setColorFilter(color);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM