[英]Android: Change Tint Color of a single EditText
我找到了有关如何以编程方式更改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的线条颜色而不全局更改线条颜色? 谢谢。
更新:我无法使用预定义的样式,因为该颜色是在应用程序运行时动态生成的。
试试这个,可以帮到你
在你的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"/>
我提供了代码以编程方式更改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);
}
我找到了解决方案。 它被发布在此 (不接受)答案中。 技巧是在从EditText中检索背景可绘制对象后对其进行突变。
这是我的工作代码:
final Drawable originalDrawable = editText.getBackground();
originalDrawable.mutate();
final Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable);
DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(darkVibrantColor));
editText.setBackground(wrappedDrawable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.