编辑:链接的问题指定了一个名为setColorFilter
的方法来删除色调。但是,对于 Button
来说,该方法不存在。
我有一个触发套接字连接的按钮。我想在等待连接状态时禁用它。我还添加了一个 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”按钮?
最佳答案
您需要调用setBackgroundTintList(null)
并将值设置为null
。
例如,这将解决您的问题
button.setBackgroundTintList(null)
或者您可以使用设置颜色
button.setBackgroundTintList(context.getResources().getColorStateList(R.color.color_name));
关于android - 禁用按钮时删除色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49577338/