![](/img/trans.png)
[英]TextInputLayout move the hint label and change it's background color when focused
[英]TextInputLayout: Different color for hint label when not focused
我想做的事:
使用嵌入在TextInputLayout中的EditText時我想...
我嘗試過的:
在TextInputLayout上將下面設置為主題/樣式確實滿足1.但不滿足2。
<style name="FloatingLabel" parent="Widget.Design.TextInputLayout">
<item name="android:textColorHint">@color/red</item>
</style>
在我的嵌入式EditText上設置特定顏色,將提示文本更改為另一種顏色:
android:textColorHint="@color/text_placeholder_gray"
當標簽從其浮動位置移回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" >
更改提示標簽顏色,但它也適用於聚焦狀態 - 這意味着4不滿足。
因為一張圖片說的超過千字(所有字段都處於非聚焦狀態):
如何實現滿足標准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>
自定義文本輸入布局:
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>
用法:
<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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.