我需要如下所示的TextInputEditText底线颜色。
Edittext为空=>灰色
我还需要TextInputLayout
的默认提示动画,因此,不能使用EditText
。我通过使用here之类的TextWatcher
实现了此功能,但无法正常工作。
这是我的代码
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/EditScreenTextInputLayoutStyle"
app:hintTextAppearance="@style/etHintText">
<android.support.design.widget.TextInputEditText
android:id="@+id/etAddress"
style="@style/et_14_blk_sngl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="text"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
样式:
<style name="EditScreenTextInputLayoutStyle">
<item name="colorControlNormal">@color/gray</item>
<item name="colorControlActivated">@color/blue</item>
<item name="colorControlHighlight">@color/blue</item></style>
然后
private void UpdateLineColor()
{
if (!TextUtils.IsEmpty(this.Text))
{
DrawableCompat.SetTint(this.Background, ContextCompat.GetColor(this.Context, Resource.Color.blue));
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Lollipop)
{
ColorStateList colorStateList = ColorStateList.ValueOf(Resources.GetColor(Resource.Color.blue));
this.BackgroundTintList = colorStateList;
ViewCompat.SetBackgroundTintList(this, colorStateList);
}
this.Background.SetColorFilter(Resources.GetColor(Resource.Color.blue), PorterDuff.Mode.SrcAtop);
}
else
{
DrawableCompat.SetTint(this.Background, ContextCompat.GetColor(this.Context, Resource.Color.gray));
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Lollipop)
{
ColorStateList colorStateList = ColorStateList.ValueOf(Resources.GetColor(Resource.Color.gray));
this.BackgroundTintList = colorStateList;
ViewCompat.SetBackgroundTintList(this, colorStateList);
}
this.Background.SetColorFilter(Resources.GetColor(Resource.Color.gray), PorterDuff.Mode.SrcAtop);
}
}
答案 0 :(得分:1)
您可以保留 TextInputEditText 的背景,例如:
自定义et_underline_selected.axml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="0dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="#00f" /> // color blue
<padding android:bottom="4dp" />
</shape>
</item>
</layer-list>
et_underline_unselected.axml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="0dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:color="#0f0"
android:width="1dp" />
<padding android:bottom="4dp" />
</shape>
</item>
</layer-list>
edittext_bg_selector.axml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="@drawable/et_underline_unselected"/>
<item android:state_focused="false"
android:drawable="@drawable/et_underline_selected"/>
</selector>
这三个文件放在资源/可绘制
中然后在您的layout.axml
中:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/etHintText">
<android.support.design.widget.TextInputEditText
android:id="@+id/etAddress"
style="@style/et_14_blk_sngl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="text"
android:background="@drawable/edittext_bg_selector"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
最后在您的activity.cs
中:
TextInputEditText etAddress = FindViewById<TextInputEditText>(Resource.Id.etAddress);
etAddress.FocusChange += (s, e) =>
{
if (e.HasFocus)
{
etAddress.SetBackgroundResource(Resource.Drawable.et_underline_selected);
}
else
{
if (etAddress.Text.Length > 0)
{
etAddress.SetBackgroundResource(Resource.Drawable.et_underline_selected);
}
else
{
etAddress.SetBackgroundResource(Resource.Drawable.et_underline_unselected);
}
}
};
您需要这种效果吗?
答案 1 :(得分:0)
将您的style
替换为:
<style name="EditScreenTextInputLayoutStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/gray</item>
<item name="colorControlActivated">@color/blue</item>
<item name="colorControlHighlight">@color/blue</item>
</style>