【问题标题】:android:state_pressed is not working , pressing the button it is not not changing the colorandroid:state_pressed 不起作用,按下按钮它不会改变颜色
【发布时间】:2021-01-12 10:06:19
【问题描述】:

我在 res/drawable 中有以下 XML 代码,并将按钮背景设置为此 drawable。但是,当我按下按钮时,它并没有改变颜色。感谢您的帮助

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/pink"/> <!-- pressed state -->
<item android:state_selected="false" android:drawable="@color/blue"/> <!-- default state -->

按钮:

        <Button
        android:id="@+id/button12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_weight="1"
        android:background="@drawable/button_bg"
        android:text="REGISTER"
        android:textColor="@color/white"
        android:textSize="20sp" />

彩色 XML :

<color name="blue">#49B8C7</color>
<color name="pink">#FF8EB9</color>

【问题讨论】:

  • 嗨,您确实在 xml 中设置了 android:state_selected 而不是 android:state_pressed。请更换它。
  • 试试&lt;item android:state_pressed="true" android:drawable="@color/pink"/&gt; &lt;!-- pressed state --&gt; &lt;item android:drawable="@color/blue"/&gt; &lt;!-- default state --&gt;
  • @Daniel.Wang state_pressed 也不起作用。
  • @rahat ,它不工作。
  • 尝试在Button标签中设置android:clickable="true"。

标签: android button colors drawable


【解决方案1】:

您的问题可能是由 android:drawable="@color/pink" 引起的。似乎不适用于可绘制的颜色。 在这种情况下,请点赞。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"> <!-- pressed state -->
        <shape android:shape="rectangle"><solid android:color="@color/pink"/></shape>
    </item>
    <item> <!-- default state -->
        <shape android:shape="rectangle"><solid android:color="@color/blue"/></shape>
    </item>
</selector>

【讨论】:

  • 按下按钮时发生按钮点击事件?
  • 选择器xml名称是buton_bg.xml
  • 请检查按钮是否启用,如果禁用,按下时背景颜色不会改变。并尝试在 Button 标签中设置 android:clickable="true"。
  • 添加了 android:clickable="true" android:enabled="true" 但仍然无法正常工作:((
  • 似乎 android:drawable="@color/pink" 在您的设备中不起作用。在这种情况下,您需要在 button_bg.xml 中使用 shape。更新答案,请检查。
【解决方案2】:

为您提供解决方案

XML 文件保存在res/color/button_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

此布局 XML 会将颜色列表应用于视图:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:textColor="@color/button_text" />

【讨论】:

  • 不使用形状可以解决吗?
  • 形状有什么问题?
  • 形状没有问题,但为什么没有形状就不行?
  • 复杂的答案。我认为您添加了不必要的代码。我需要它在没有侦听器和 java 代码的情况下工作。它应该适用于简单的 xml 文件。
  • 这里有什么复杂的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 2013-09-24
  • 2016-11-28
相关资源
最近更新 更多