【问题标题】:Do not move hint to the top when calling setError() of TextInputLayout调用 TextInputLayout 的 setError() 时不要将提示移到顶部
【发布时间】:2018-02-03 02:53:11
【问题描述】:

我有一个带有 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 会有焦点一样。

我想在设置错误时保持提示不变。它应该看起来像这样:

任何想法如何构建此布局?

【问题讨论】:

  • TextInputLayout 在代码中的外观如何?
  • 我添加了我的xml文件的内容
  • @buellas 你需要输出上图的权利。

标签: android xml android-layout android-textinputlayout


【解决方案1】:

你可以用其他方式来做,你可以打电话给setError()OnCreate()

 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);不需要在 OnCreate() 上调用 setError()。
【解决方案2】:

您需要实现自己的 MyTextInputLayout 类,该类从 TextInputLayout 扩展而来。

在该类中,您必须控制何时显示或不显示提示,覆盖出现错误时显示提示的自定义行为。

SO 另一个答案的好起点:https://stackoverflow.com/a/44521653/2174489

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2021-10-02
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多