如何设置pssed按钮的STATE_ $ P $ [英] How to set the state_pressed of a button

查看:115
本文介绍了如何设置pssed按钮的STATE_ $ P $的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个xml文件上色我的按钮。在XML中有这样的code单击时来定义我的按钮的颜色。

I use a xml file to color my button. In the xml a have this code to define the color of my button when it's clicked.

    <?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="@color/mainRaddoppiaButtonBackgroundEnd"
                android:endColor="@color/mainRaddoppiaButtonBackgroundStart"

                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/mainRaddoppiaButtonBackgroundStart" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
           <shape>
            <gradient
                  android:startColor="@color/mainRaddoppiaButtonBackgroundEnd"
                android:endColor="@color/mainRaddoppiaButtonBackgroundStart"

                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/mainRaddoppiaButtonBackgroundStart" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:startColor="@color/mainRaddoppiaButtonBackgroundStart"
                android:endColor="@color/mainRaddoppiaButtonBackgroundEnd"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/mainRaddoppiaButtonBackgroundStart" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

难道不可能性从Java到让我的按钮pressed和STATE_ pressed的价值观在我的XML文件中设置?它颜色

Is it possibile from java to keep my button pressed and color it with "state_pressed" values set in my xml file?

成才这样的:

  public void onClick(View button) {
     button.seLayout(R.xml.xmlFileName.state_pressed)
}

当然,这code有没有意义,我只是做它让你明白我想做的事情。

Of course this code has no sense, i just made it up to let you understand what i want to do

推荐答案

试试这个。

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

这篇关于如何设置pssed按钮的STATE_ $ P $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆