TextInputEditText 自定义样式问题(包含文本时)
TextInputEditText custom style issue (When contain text)
我需要如下所示的 TextInputEditText 底线颜色。
默认颜色 => 灰色
如果用户输入文本,线条颜色应保持为蓝色。目前,如果我
在第一个编辑文本中输入输入并转到其他编辑文本,第一个
又变灰了
Edittext 为空 => 灰色
我还需要TextInputLayout
的默认提示动画,所以不能使用EditText
。我通过使用 TextWatcher
像 here 但没有工作来实现这个。
这是我的代码
<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);
}
}
将您的 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>
你可以自定义 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>
这三个文件放在Resources/drawable
然后在你的 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);
}
}
};
你需要这种效果吗?
我需要如下所示的 TextInputEditText 底线颜色。
默认颜色 => 灰色
如果用户输入文本,线条颜色应保持为蓝色。目前,如果我 在第一个编辑文本中输入输入并转到其他编辑文本,第一个 又变灰了
Edittext 为空 => 灰色
我还需要
TextInputLayout
的默认提示动画,所以不能使用EditText
。我通过使用TextWatcher
像 here 但没有工作来实现这个。
这是我的代码
<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);
}
}
将您的 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>
你可以自定义 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>
这三个文件放在Resources/drawable
然后在你的 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);
}
}
};
你需要这种效果吗?