在 Android 中更改按钮背景颜色后无触摸反馈

No Touch feedback after changing Button background color in Android

我正在使用 Android API 21 和 Android Studio。

我更改了按钮的背景颜色。它不再像之前那样提供触摸反馈。

我一直在寻找解决方案并找到了创建 drawables 的方法,但是我在 android 开发者网站上阅读了有关触摸反馈的信息并找到了这些:

https://developer.android.com/design/style/touch-feedback.html

Incorporating your branding is much easier because the default touch feedback works with whatever hue you choose.

https://developer.android.com/design/style/branding.html

When customizing colors, touch feedback should be subtle — just slightly lighter or darker than the untouched color.

这是否意味着即使我自动更改背景,触摸反馈也会起作用?或者我需要做一些工作才能让它工作吗?

此外,如果我需要手动重现反馈,我该怎么做(比最新 Android Studio 中的可绘制对象更好的方法)?

刚开始接触android,很迷茫。感谢你的帮助。谢谢。

您需要在可绘制对象中明确设置触摸反馈(按钮 按下 状态)。改变背景意味着失去反馈(即按下)状态。

切换具有 feedback/pressed 状态的 Button 背景的唯一方法是在 Drawables 中定义各种 Button 定义。这将使切换背景变得容易,只需一个参考按钮的所有状态(禁用、按下、聚焦和启用)。

方法 setBackgroundResource() 仅将引用的资源设置为 所有 按钮的状态。因此,最好在 Drawable 中定义一个 Button 定义,并明确显示其所有状态,并使用它们进行切换。 setBackgroundColor() 也是如此,它应用背景颜色覆盖按钮的所有状态,并且只应在需要时使用(对于大多数一般情况不应使用)。

我不使用 Android Studio。我使用老式的基于 Eclipse 的 ADT,但是,我确信没有不使用 Drawables 的解决方法,因为按钮状态和背景 Android 应用程序开发在各种 IDE 上标准化。

通过可绘制对象实现 Button 定义非常容易,这里有一个示例:

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

<!-- Button Disabled -->
<item
    android:state_enabled="false">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <gradient
            android:startColor="#F4F4F4"
            android:centerColor="#A6A6A6"
            android:endColor="#F4F4F4"
            android:angle="90"/>
        <padding android:left="7dp"
            android:top="7dp"
            android:right="7dp"
            android:bottom="7dp" />
        <stroke
            android:width="2dip"
            android:color="#FFFFFF" />
        <corners android:radius= "8dp" />
    </shape> 
</item>

<!-- Button Pressed / Feedback -->
<item
    android:state_pressed="true"
    android:state_enabled="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <gradient
            android:startColor="#2222EE"
            android:centerColor="#22EE11"
            android:endColor="#2222EE"
            android:angle="90"/>
        <padding android:left="7dp"
            android:top="7dp"
            android:right="7dp"
            android:bottom="7dp" />
        <stroke
            android:width="2dip"
            android:color="#FFFFFF" />
        <corners android:radius= "8dp" />
    </shape> 
</item>

<!-- Button Focused -->
<!-- Sometimes developers define Shapes in a separate drawable. The code is the same. Here's an example. -->
<item
    android:state_focused="true"
    android:state_enabled="true"
    android:drawable="@drawable/button_focused"> <!-- Refers to another drawable file with name "button_focused" -->
</item>

<!-- Button Enabled -->
<item
    android:state_enabled="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <gradient
            android:startColor="#22EE11"
            android:centerColor="#2222EE"
            android:endColor="#22EE11"
            android:angle="90"/>
        <padding android:left="7dp"
            android:top="7dp"
            android:right="7dp"
            android:bottom="7dp" />
        <stroke
            android:width="2dip"
            android:color="#FFFFFF" />
        <corners android:radius= "8dp" />
    </shape>
</item>

</selector>

将 Button 定义拆分为多个 drawable 有利也有弊。最好在一个 drawable 中保留一个 Button 定义。

祝你好运!

这个问题最简单的解决方案是 而不是

android:background="@color/myColor"

使用这条线

android:backgroundTint="@color/myColor"