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