按钮单击效果,而不在Android中有另一张图片

时间:2012-08-26 13:17:32

标签: java android button

我的应用程序中的按钮是常规按钮,但具有自定义背景(请参阅此内容以了解我的意思http://www.gersic.com/blog.php?id=56)。 一切正常。但是,我想向用户表明他正在按下按钮而没有另一个按下"按钮上的版本(我知道如何在按下时给按钮另一个图像)

我目前使用onTouchListner并在释放时按下并清除时在按钮上应用colorFilters但在触摸模式下检测到按下并准确* *并不适合我

修改

这就是我想要的方式。

没有按下: not pressed

按下(相同的图像文件): pressed

我使用colorFilter进行了高光效果                  getBtn.getBackground().setColorFilter(0xFF838B83, PorterDuff.Mode.MULTIPLY);

1 个答案:

答案 0 :(得分:0)

您可以为按钮创建一个选择器,这样您就可以将其设置为背景,但只要单击它就会产生单击效果。

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="insert color"
                android:endColor="insert color"
                />
            <stroke
                android:width="3dp"
                android:color="insert color" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>



    <item android:state_pressed="false" >        
        <shape>
            <gradient
                android:endColor="insert color"
                android:startColor="insert color"
               />
            <stroke
                android:width="3dp"
                android:color="insert color" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

将此xml文件设置为按钮的背景:

mybtn.setbackground(R.drawable.thexmlfileabove);