1
字符計數器在最大長度時更改浮動標籤顏色,但調用setError()
不會更改它。如何在調用setError()時更改TextInputLayout的浮動標籤顏色
我試過app:errorTextAppearance="@style/MyErrorText"
。
風格/ MyErrorText:
<style name="MyErrorText" parent="TextAppearance.AppCompat.Small">
<item name="android:textColor">@color/error_color</item>
</style>
而且我也試過setErrorEnabled(true)
。但是,他們沒有改變。撥打setError()
時有沒有辦法改變浮動標籤的顏色?
XML代碼:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/spacing_big"
android:paddingRight="@dimen/spacing_big"
android:paddingTop="@dimen/spacing_small">
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="25"
app:theme="@style/CustomTilTheme"
android:layout_marginBottom="@dimen/spacing_small">
<android.support.design.widget.TextInputEditText
android:id="@+id/list_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name_hint"
android:textSize="@dimen/text_normal">
<requestFocus/>
</android.support.design.widget.TextInputEditText>
</android.support.design.widget.TextInputLayout>
......
</LinearLayout>
</ScrollView>
風格/ CustomTilTheme:
<style name="CustomTilTheme" parent="Theme.Design.Light">
<item name="colorControlActivated">@color/accent_color</item>
</style>
java代碼:
......
final TextInputLayout textInputLayoutName = (TextInputLayout) dialogLayout.findViewById(R.id.text_input_layout_name);
textInputEditText = (TextInputEditText) dialogLayout.findViewById(R.id.list_name);
textInputEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
int size = s.length();
if (size < 1) {
textInputLayoutName.setError("1文字以上入力してください");
} else if (size > 25) {
textInputLayoutName.setError("25文字以內で入力してください");
} else {
textInputLayoutName.setError(null);
}
}
});
if (args == null) {
textInputLayoutName.setError("1文字以上入力してください");
} else {
textInputEditText.setText(args.getString("list_name"));
}
......
我使用
- 支持庫23.3.0
- SDK工具25.1.3
- SDK平臺工具23.1
- 生成工具23.0.3
這個應用程序在Nexus 5 API23運行(官方Android模擬器) 。
你可以試試這個鏈接,希望它可以幫助你。 http://stackoverflow.com/a/32818303/5744335 – Arshak