Макет ввода текста Android
У меня есть макет ввода текста, и я хочу использовать его для отображения сообщения об ошибке, если данные, введенные в текст для редактирования внутри него, неверны. Определения следующие:
<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();
Когда все это сделано, я получаю ошибку:
Can't convert to color: type=0x2
на линии numberInputLayout.setErrorEnabled(true);
Куда я иду не так?
РЕДАКТИРОВАТЬ 1: как только я удаляю TextLabelWhite
тема, она начинает работать. Но тема нужна.
3 ответа
+ Изменить
android:theme="@style/TextLabelWhite"
в
style="@style/TextLabelWhite"
в android.support.design.widget.TextInputLayout атрибуты в вашем XML, и вы не получите ошибку.
ОБНОВЛЕНИЕ: чтобы настроить цвета, вы можете попробовать это. добавлять
<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"
При первом звонке view
вместо getView()
не
EditText numberEditText = (EditText) getView().findViewById(R.id.number_edit_text);
Делать
EditText numberEditText = (EditText) view.findViewById(R.id.number_edit_text);
Просто попробуйте это один раз и позвольте мне знать: без TextInputLayout
EditText numberEditText = (EditText) getView().findViewById(R.id.number_edit_text);
String strNumber = shipText.getText().toString();
if(TextUtils.isEmpty(strNumber )) {
numberEditText .setError("Please enter a value");
}
Или сделать:с TextInputLayout
TextInputLayout numberInputLayout = (TextInputLayout) view.findViewById(R.id.number_input_layout);
EditText numberEditText = (EditText) getView().findViewById(R.id.number_edit_text);
**numberInputLayout.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);**
numberInputLayout.setErrorEnabled(true);
***numberInputLayout.setError("Tis an error dear");***
Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();