Android TextInputLayout:未聚焦时提示标签的不同颜色
我想做什么: 当使用嵌入在TextInputLayout中的EditText时,我想Android TextInputLayout:未聚焦时提示标签的不同颜色,android,android-layout,android-theme,android-styles,Android,Android Layout,Android Theme,Android Styles,我想做什么: 当使用嵌入在TextInputLayout中的EditText时,我想 由于用户已经输入了某个值,因此当标签取消聚焦并浮动在编辑文本上方时,将其颜色设置为绿色 由于用户尚未输入值,因此当标签取消聚焦并位于EditText内时,将其颜色设置为红色 我不想将所有EditText的提示文本颜色更改为红色,但仅当它们包装在TextInputLayout中时才可以(我不需要一般的方法-在布局XML中为每个TextInputLayout设置主题/样式这样的特定方法就可以了) 当用户聚焦字段时,
<style name="FloatingLabel" parent="Widget.Design.TextInputLayout">
<item name="android:textColorHint">@color/red</item>
</style>
当标签作为提示(即无文本)从其浮动位置移回Edittext时,实际上会导致提示文本重叠
设置此选项:
<style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/main_color</item>
@颜色/主色调
在TextInputLayout上:
<android.support.design.widget.TextInputLayout
...
app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout" >
如何实现满足标准1-4的设置?我有一个类似的问题:我需要实现一个文本输入布局,其中标签有不同的颜色表示空(当编辑文本中没有输入文本时)、“填充”和聚焦状态。我的主要问题是如何区分空状态和填充状态,因为使用选择器为聚焦状态设置不同的颜色已经很容易了。最后,我决定定义一个自定义的“空文本”状态,并实现我的自定义文本输入布局(它扩展了正常的文本输入布局)
下面是一些代码:
在res/values/attrs.xml中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<!-- Custom state for the text input layout to determine whether the label is shown above some text or not -->
<declare-styleable name="EmptyTextState">
<attr name="state_empty_text" format="boolean"/>
</declare-styleable>
</resources>
XML选择器,用于设置不同状态下标签的颜色(res/color/input_field_floating_label.XML):
输入文本布局的样式(在res/values/styles.xml中):
...
@颜色/输入\字段\浮动\标签
编辑文本的主题和样式(仍在res/values/styles.xml中):
...
@颜色/输入\字段\浮动\标签
@样式/编辑文本主题
...
用法:
<com.package.path.widget.EmptyStateTextInputLayout
style="@style/DarkEditTextLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
...
>
<EditText
style="@style/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.package.path.widget.EmptyStateTextInputLayout>
我推荐这篇博文来了解如何使用自定义状态:当我这样做时,EmptyStateTextInputLayout结果是空的。我错过了什么?(我真的需要它来工作,这是我最接近的解决方案)
public class EmptyStateTextInputLayout extends TextInputLayout {
private boolean emptyText = true;
private static final int[] EMPTY_TEXT_STATE = new int[]{R.attr.state_empty_text};
public EmptyStateTextInputLayout(Context context) {
super(context);
}
public EmptyStateTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EmptyStateTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
int[] state = super.onCreateDrawableState(extraSpace + 1);
if (emptyText) {
mergeDrawableStates(state, EMPTY_TEXT_STATE);
}
return state;
}
public void setEmptyTextState(boolean emptyTextState) {
this.emptyText = emptyTextState;
refreshDrawableState();
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof EditText) {
EditText editText = (EditText) child;
if (!TextUtils.isEmpty(editText.getText())) {
setEmptyTextState(false);
}
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!TextUtils.isEmpty(editable)) {
setEmptyTextState(false);
} else {
setEmptyTextState(true);
}
}
});
}
super.addView(child, index, params);
}
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:color="@color/focused_text_color" android:state_focused="true" />
<item android:color="@color/placeholder_color" app:state_empty_text="true"/>
<item android:color="@color/primary_text_color"/> <!-- default color -->
</selector>
<style name="EditTextLayout">
...
<item name="android:textColorHint">@color/input_field_floating_label</item>
</style>
<style name="EditTextTheme">
...
<item name="android:textColorHint">@color/input_field_floating_label</item>
</style>
<style name="EditText">
<item name="android:theme">@style/EditTextTheme</item>
...
</style>
<com.package.path.widget.EmptyStateTextInputLayout
style="@style/DarkEditTextLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
...
>
<EditText
style="@style/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.package.path.widget.EmptyStateTextInputLayout>