简体   繁体   English

如何更改按钮状态直到按下另一个按钮?

[英]How do I change a button state until another button is pressed?

I have a button that has a drawable XML file set as the background, this is so that when the button is pressed the background colour and outline of the button is change. 我有一个将可绘制的XML文件设置为背景的按钮,这样可以在按下按钮时更改背景颜色和按钮的轮廓。 However it does not stay like this, after a second it goes back to what it is before. 但是,它并不会像这样保持,在一秒钟后又回到了之前的状态。 How do I get it to stay with the changed colours until a different button is pressed? 如何使它保持更改的颜色,直到按下另一个按钮?

<item android:state_pressed="true" >
    <shape>
        <solid
            android:color="#99000000" />
        <stroke
            android:width="1dp"
            android:color="#489d73" />
    </shape>
</item>
<item>
    <shape>
        <gradient
            android:startColor="#66000000"
            android:endColor="#66000000"
            android:angle="270" />

    </shape>
</item>

Create background drawables for each button state. 为每个按钮状态创建背景可绘制对象。 Then when clicking another button change view's background programmatically. 然后,当单击另一个按钮时,以编程方式更改视图的背景。

如果希望按钮在更改之前保持其颜色,则必须以编程方式更改其背景。

您可以像这样以编程方式更改按钮背景。

btn.setBackgroundResource(R.drawable.buttondrawable)

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

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