Android更改单个编辑文本下划线颜色
我试图以编程方式更改单个EditText的全息下划线颜色。我已经尝试了所有的例子,我可以找到这样,但似乎没有任何工作。以下是我最新也是最好的尝试: 编辑:当前代码:Android更改单个编辑文本下划线颜色,android,xamarin,xamarin.android,Android,Xamarin,Xamarin.android,我试图以编程方式更改单个EditText的全息下划线颜色。我已经尝试了所有的例子,我可以找到这样,但似乎没有任何工作。以下是我最新也是最好的尝试: 编辑:当前代码: txtName.Background.ClearColorFilter(); txtName.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#ff0000"), PorterDuff.Mode.SrcIn); 我也尝试过只使用txtName.Backgro
txtName.Background.ClearColorFilter();
txtName.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#ff0000"), PorterDuff.Mode.SrcIn);
我也尝试过只使用txtName.Background.SetTint(Resource.Color.colorRed)
,但也不起作用
下面是我试图更改的线条颜色的图片:
EditText XML:
<EditText
android:id="@+id/input_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
android:maxLength="30"
android:singleLine="true"
android:scrollHorizontally="true"
android:hint="Name"
android:textColor="#8c8c8c"
android:textColorHint="#8c8c8c"
app:backgroundTint="#22d6d3"
android:layout_marginBottom="10dp" />
如本文所述:
要设置颜色,请执行以下操作:
editText.getBackground().setColorFilter(颜色,
PorterDuff.Mode.SRC_IN)要删除颜色,请执行以下操作: editText.getBackground().clearColorFilter() 还有其他变化,如
我已经测试了setColorFilter,它对我的应用程序有效。感谢@Large的回答,他的回答在使用java的本机Android上运行良好 在Xamarin.Android中,它与本机Android相同,请执行以下操作:
txtName.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#ff0000"), PorterDuff.Mode.SrcIn);
txtName.Background.SetColorFilter(Color.Red, PorterDuff.Mode.SrcIn);
或者使用android的颜色资源,如下所示:
txtName.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#ff0000"), PorterDuff.Mode.SrcIn);
txtName.Background.SetColorFilter(Color.Red, PorterDuff.Mode.SrcIn);
不要使用新颜色(Resource.Color.colorRed),因为该方法在Android中已被弃用。更新
仅在使用Appcompat库时有效
我建议你试试这个
ViewCompat.SetBackgroundTintList(_YourView , ColorStateList.ValueOf(Color.ParseColor(#ff0000))));
在您的例子中,“YourView”表示您要更改其颜色的编辑文本,其值采用Android图形颜色,因此易于使用
如果要支持Android API-19或更低版本,另一个建议是使用appcompat EditText。您可以为EditText的格式创建自定义呈现程序。
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Background = new ColorDrawable(Android.Graphics.Color.Transparent);
}
}
protected override void OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
Control.Background=新的可着色(Android.Graphics.Color.Transparent);
}
}
在我的代码中尝试了这一点:txtName.Background.SetColorFilter(新颜色(Resource.Color.colorRed),PorterDuff.Mode.SrcIn)代码>但仍然不会更改颜色。我需要在添加新的颜色之前清除颜色吗?我使用了您的精确代码,并在运行API 21的模拟器上运行。仍然没有改变颜色。我还设置了断点,以确保代码被正确命中,事实就是如此。回到另一个答案中的问题,在添加红色之前是否需要清除颜色过滤器?您可以使用txtName.Background.ClearColorFilter()
尝试一下,但我不确定,你能给我看看你的编辑文本和.axml
?是的,清除也不起作用。我用XML和当前代码更新了帖子。您好,我们使用的是EditText
而不是appcompatieditText
,请在您的min版本和其他版本上尝试EditText
类。将其更改为常规EditText。。还是没什么。编辑:只是为了澄清,我也确定并使用EditText作为代码中的类型。我已经回答了,试试看