How To make a small flash when a button is tapped?

[亡魂溺海] 提交于 2020-01-02 05:18:07

问题


So I'm making an android app that has more than 100 buttons,but you know when you tap a button normally when you don't changed the background or anything it flashes orangish color. However since I've added a background color to my buttons when they're tapped it just goes to the next screen and you can't tell that you've tapped a Button!

Could someone help me please? Sorry if I don't know what those are called:(


回答1:


Declare in drawables this selector and name it for example: button.xml

 <?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/btnPressed"/>
    <item  android:drawable="@drawable/btnNormal"></item>

</selector>

android:drawable can be color, image, another drawable... And then you can declare your button as:

<Button 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/button"
     />

If you create your buttons in code you can call method: setBackgroundResource() and pass resource id. Example:

Button button = new Button(this);
button.setBackgroundResource(R.drawable.button);

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList




回答2:


    alphaDown = new AlphaAnimation(1.0f, 0.3f);
    alphaUp = new AlphaAnimation(0.3f, 1.0f);
    alphaDown.setDuration(1000);
    alphaUp.setDuration(500);
    alphaDown.setFillAfter(true);
    alphaUp.setFillAfter(true);
    analyse.startAnimation(alphaUp);

try this code on your button click




回答3:


Use android selectors. Create new drawable with its own states and set it as button's background drawable



来源:https://stackoverflow.com/questions/9855493/how-to-make-a-small-flash-when-a-button-is-tapped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!