我找到了关于如何以编程方式更改Android中EditText的线条颜色的各种答案。 现在我正在使用这个解决方案:
final Drawable originalDrawable = editText.getBackground();
final Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable);
DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(darkVibrantColor));
editText.setBackground(wrappedDrawable);
这实际上改变了EditText的颜色,但遗憾的是它不仅改变了我正在使用的特定EditText的线条颜色,还改变了我的应用程序中使用的所有EditTexts的线条颜色。活动,如果他们在不同的活动。
如何在不更改全局线条颜色的情况下更改一个特定EditText的线条颜色?谢谢。
更新:我无法使用预定义的样式,因为颜色是在应用程序运行时动态生成的。
答案 0 :(得分:0)
试试这个,可以帮到你
在你的style.xml中
<style name="MyEditText" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">@color/indigo</item>
<item name="colorControlActivated">@color/pink</item>
<item name="android:padding">20dp</item>
<item name="android:textSize">16dp>
<item name="android:editTextColor">@color/black</item>
<item name="android:tint">@color/colorPrimary</item>
</style>
在你的edittext中添加你的风格
<EditText
style="@style/MyEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
答案 1 :(得分:0)
我提供了代码以以编程方式更改EditText的下划线颜色。
EditText editTextOne=(EditText) findViewById(R.id.edit_text_one);
Drawable drawable=editTextOne.getBackground();
int color=Color.parseColor("#FFFFFF");
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
if (Build.VERSION.SDK_INT > 16) {
editTextOne.setBackground(drawable);
} else {
editTextOne.setCompoundDrawables(null,null,drawable,null);
}
答案 2 :(得分:0)
我找到了解决方案。它发布在this(未接受)答案中。诀窍是在从EditText检索后绘制背景drawable。
这是我的工作代码:
final Drawable originalDrawable = editText.getBackground();
originalDrawable.mutate();
final Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable);
DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(darkVibrantColor));
editText.setBackground(wrappedDrawable);