我有一个文本输入布局,如果在其中的编辑文本中输入的数据不正确,我想使用它来显示错误消息.定义如下:
<android.support.design.widget.TextInputLayout android:id="@+id/number_input_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/TextLabelWhite" > <EditText android:id="@+id/number_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:digits="01234 56789" android:hint="Number" android:inputType="number" android:textColor="@color/white" /> </android.support.design.widget.TextInputLayout>
样式定义如下:
<style name="TextLabelWhite" parent="TextAppearance.AppCompat"> <item name="android:textColorHint">@color/white</item> <item name="colorAccent">@color/white</item> <item name="colorControlnormal">@color/white</item> <item name="colorControlActivated">@color/white</item> </style>
现在,如果输入的数据不正确,我会执行以下操作:
TextInputLayout numberInputLayout = (TextInputLayout) view.findViewById(R.id.number_input_layout); EditText numberEditText = (EditText) getView().findViewById(R.id.number_edit_text); numberEditText.getBackground().setColorFilter(Color.RED,PorterDuff.Mode.SRC_ATOP); numberInputLayout.setErrorEnabled(true); numberEditText.setError("Tis an error dear"); Toast.makeText(getActivity(),error,Toast.LENGTH_SHORT).show();
完成所有这些后,我得到错误:
无法转换为color:type = 0x2,行numberInputLayout.setErrorEnabled(true);
我哪里错了?
解决方法
更改
android:theme="@style/TextLabelWhite"
至
style="@style/TextLabelWhite"
在xml中的android.support.design.widget.TextInputLayout属性中,您将不会收到错误.
更新:要自定义颜色,您可以尝试这个.加
<item name="colorControlnormal">@android:color/white</item> <item name="colorControlActivated">@android:color/white</item>
直接到您的应用主题样式.它会影响所有EditText,但如果它对您的应用程序不是问题,它可能会有所帮助.
更新2:
我找到的最好的解决方案.使用
android:theme="@style/TextLabelWhite"
就像你的xml一样.将TextLabelWhite样式父级更改为AppTheme样式,如:
<style name="TextLabelWhite" parent="AppTheme">
但android:textColorHint不适用于TextInputLayout(没有这样的属性).您应该使用design:hintTextAppearance,但将其置于不同的样式并直接应用于TextInputLayout
design:hintTextAppearance="@style/CustomTextAppearance"
原文地址:https://www.jb51.cc/android/317493.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。