gpt4 book ai didi

Android文本输入布局

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:12:31 25 4
gpt4 key购买 nike

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

<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 主题,它就会开始工作。但是主题是必须的。

最佳答案

改变

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"

关于Android文本输入布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34853869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com