我遇到了类似的问题:我需要实现一个文本输入布局,其中标签对于空(当编辑文本中没有输入文本时)、“填充”和聚焦状态具有不同的颜色。我的主要问题是如何区分空状态和填充状态,因为使用选择器已经很容易为聚焦状态设置不同的颜色。最后,我决定定义一个自定义的“空文本”状态并实现我的自定义文本输入布局(它扩展了正常的文本输入布局)。
这是一些代码:
在 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>
自定义文本输入布局:
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 选择器 (res/color/input_field_floating_label.xml):
<?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>
输入文本布局的样式(在 res/values/styles.xml 中):
<style name="EditTextLayout">
...
<item name="android:textColorHint">@color/input_field_floating_label</item>
</style>
编辑文本的主题和样式(仍在 res/values/styles.xml 中):
<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>
Usage:
<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>
我推荐这篇博文来了解如何使用自定义状态:http://code.neenbedankt.com/example-of-custom-states-in-android-components/ http://code.neenbedankt.com/example-of-custom-states-in-android-components/