调用TextInputLayout的setError()方法时不要将提示移到顶部

3

我有一个带有TextInputLayout的布局文件:

<android.support.design.widget.TextInputLayout
    android:id="@+id/fullname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorEnabled="true"
    app:errorTextAppearance="@style/Error">

    <EditText
        android:id="@+id/fullnameEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Full Name"
        android:inputType="textEmailAddress"
        android:padding="26dp"/>
</android.support.design.widget.TextInputLayout>

当我在TextInputLayout上调用setError()方法时,提示会像TextInputLayout有焦点一样移到顶部。
我希望在设置错误时保留提示。它应该看起来像这样:

design

有任何想法如何构建这个布局吗?

TextInputLayout在代码中是什么样子的? - Nachi
我添加了我的 XML 文件的内容。 - buellas
@buellas,您需要上述图像的输出对吧。 - Ratilal Chopda
2个回答

1

您可以采用另一种方式来实现,可以在OnCreate()中调用setError()

 mTextInputLayout = findViewById(R.id.fullname);
 mTextInputLayout.setError("Enter Your Full Name");

当你想要显示错误时,这样做:

mTextInputLayout.setErrorEnabled(true);

这将解决您在提示方面的问题。
   if (mTextInputLayout.isErrorEnabled()){
         mTextInputLayout.setHintEnabled(false);
      }else {
         mTextInputLayout.setHintEnabled(true);
      }

非常感谢!重要的是调用mTextInputLayout.setHintEnabled(boolean);setError()不需要在OnCreate()中调用。 - buellas

0

你需要实现自己的MyTextInputLayout类,继承自TextInputLayout

在这个类中,你需要控制何时显示或隐藏提示,覆盖默认行为,在错误出现时显示提示。

在Stack Overflow的另一个答案中有一个不错的起点:https://dev59.com/1JXfa4cB1Zd3GeqPbjMy#44521653


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接