android - 禁用按钮时删除色调

标签 android button

编辑:链接的问题指定了一个名为setColorFilter的方法来删除色调。但是,对于 Button 来说,该方法不存在。

启用按钮: enter image description here

我有一个触发套接字连接的按钮。我想在等待连接状态时禁用它。我还添加了一个 backgroundTint 来向按钮添加颜色,而无需更改基本按钮提供的默认魔法。 (如果我设置 background 而不是 backgroundTint,它将被删除)

<Button
    android:id="@+id/connectToServer"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:backgroundTint="@color/colorPrimary"
    android:text="@string/connect"
    android:textColor="@android:color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/guideline5"
    app:layout_constraintStart_toStartOf="@+id/guideline4"
    app:layout_constraintTop_toBottomOf="@+id/preset2"
    app:layout_constraintVertical_bias="0.100000024" />

这就是我禁用它的方法:

connectToServer.setEnabled(false);

但是,由于设置了 backgroundTint,该按钮被禁用,但它看起来仍然存在。 (当然,它不能再点击了,但这很糟糕的用户体验。)

如何解决这个问题,而不使用“Make-Your-Own-Colored-Drawable”按钮?

这就是我正在尝试构建的。仅限彩色。 Credits: UX Planet

最佳答案

您需要调用setBackgroundTintList(null)并将值设置为null

例如,这将解决您的问题

button.setBackgroundTintList(null) 

或者您可以使用设置颜色

button.setBackgroundTintList(context.getResources().getColorStateList(R.color.color_name));

关于android - 禁用按钮时删除色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49577338/

相关文章:

Android:处理 Activity 堆栈

html - 里面有图标的按钮

java - 如何在 Java 中加密 Paypal 按钮

.net - 读取按钮的 'Enabled' 状态

c# - 如何使按钮看起来好像被按下了?

android - 在 Android Studio 中构建 apk 时,APK 大小大幅增加,即从 3 mb 增加到 20mb

java - SimpleDateFormat.getTimeInstance 忽略 24 小时格式

Android Firebase 后台通知未触发

android - 图库 - 通过触摸滚动不起作用

用于检查所有复选框的 jQuery 按钮在启动时消失