Android文字输入布局

Pra*_*ava 7 android android-edittext

我有一个文本输入布局,如果在其中的编辑文本中输入的数据不正确,我想使用它来显示错误消息.定义如下:

<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>
Run Code Online (Sandbox Code Playgroud)

样式定义如下:

<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>
Run Code Online (Sandbox Code Playgroud)

现在,如果输入的数据不正确,我会执行以下操作:

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();
Run Code Online (Sandbox Code Playgroud)

完成所有这些后,我得到错误:

Can't convert to color: type=0x2 在线上 numberInputLayout.setErrorEnabled(true);

我哪里错了?

编辑1:一旦我删除TextLabelWhite主题,它就开始工作.但这个主题是必要的.

j2e*_*esu 16

更改

android:theme="@style/TextLabelWhite"
Run Code Online (Sandbox Code Playgroud)

style="@style/TextLabelWhite"
Run Code Online (Sandbox Code Playgroud)

在xml中的android.support.design.widget.TextInputLayout属性中,您将不会收到错误.

更新:要自定义颜色,您可以尝试这样做.加

<item name="colorControlNormal">@android:color/white</item>
<item name="colorControlActivated">@android:color/white</item>
Run Code Online (Sandbox Code Playgroud)

直接到您的应用程序主题样式.它会影响所有EditText,但如果它对您的应用程序不是问题,它可能会有所帮助.

更新2:

我找到的最佳解决方案.使用

android:theme="@style/TextLabelWhite"
Run Code Online (Sandbox Code Playgroud)

就像你的xml一样.将TextLabelWhite样式父级更改为AppTheme样式,如:

<style name="TextLabelWhite" parent="AppTheme">
Run Code Online (Sandbox Code Playgroud)

但是android:textColorHint不适用于TextInputLayout(没有这样的属性).您应该使用design:hintTextAppearance,但是将它放在不同的样式中并直接应用于TextInputLayout

design:hintTextAppearance="@style/CustomTextAppearance"
Run Code Online (Sandbox Code Playgroud)


Int*_*iya 2

第一次调用view而不是getView()

EditText numberEditText = (EditText) getView().findViewById(R.id.number_edit_text);
Run Code Online (Sandbox Code Playgroud)

EditText numberEditText = (EditText) view.findViewById(R.id.number_edit_text);
Run Code Online (Sandbox Code Playgroud)