【问题标题】:how to change button color when it is clicked in android在android中单击时如何更改按钮颜色
【发布时间】:2011-08-09 07:54:44
【问题描述】:

默认情况下,单击按钮时,橙色之类的东西会在按钮周围短时间环绕,表示按钮已被单击。但是当按钮包含背景图像时,此功能不起作用。这也发生在列表视图中。为什么?有任何想法吗? TIA
我使用了 setBackgroundColor(Color.BLUE); 但是这里的颜色是应用的并且没有消失......

【问题讨论】:

    标签: android button widget


    【解决方案1】:

    我也有同样的问题。所以我没有设置背景颜色,而是包含了三种不同颜色的按钮的三个图像,所以根据状态聚焦,按下,默认相应的图像将替换另一个。而且看起来按钮的颜色发生了变化。

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

    上面是xml文件的内容,必须分配给各个按钮的背景 android:background="@drawable/drawable_button

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      您需要使用选择器作为背景资源:

      <?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/button_pressed" />
          <item android:state_focused="true" android:drawable="@drawable/button_focus" />
          <item android:drawable="@drawable/button_normal" />
      </selector>
      

      此选择器使用 3 个独立的可绘制对象来更改按钮的图像,您可以使用相同的图像进行按下和聚焦。

      您需要将此 XML 放入您的 drawables 目录,并将其用作按钮的背景。

      其他解决方案请参考:Standard Android Button with a different color

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-07
        • 2021-07-21
        • 2019-05-20
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 2021-07-07
        • 1970-01-01
        相关资源
        最近更新 更多