我有一个普通的 EditText
字段,我想以编程方式更改其下划线颜色。
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
其他答案建议像这样更改背景颜色过滤器:
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
但是,当我运行该应用程序时,我没有看到任何变化。改变背景本身:
editText.setBackground(color)
将整个 EditText
更改为 color
- 这不是我想要的!
如何以编程方式更改 EditText
、AppCompatEditText
或 TextInputEditText
的下划线颜色?我使用的是支持库的 25.0.1 版。
最佳答案
@degs 的回答是正确的。但只需添加一个小注释:AppCompatEditText#setSupportBackgroundTintList
现在带有 @RestrictTo(LIBRARY_GROUP)
注释,这意味着:
改为使用 ViewCompat#setBackgroundTintList
.所以在你的例子中,它应该是这样的:
ColorStateList colorStateList = ColorStateList.valueOf(color);
ViewCompat.setBackgroundTintList(editText, colorStateList);
关于android - 以编程方式更改 EditText 的下划线颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40838069/