TextInputLayout和TextInputEditText有什么区别?

32

想要了解TextInputEditText和TextInputLayout之间的实际区别,以及何时应该使用它们中的一个。

3个回答

36

它们是互补功能的不同布局。

  • TextInputLayout扩展了LinearLayout
  • TextInputEditText扩展了EditText

它们旨在像以下方式一起使用:

<TextInputLayout>
   <TextInputEditText/>
</TextInputLayout>

官方文档中已经包含了所有内容:

TextInputLayout:

https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html

此布局将 EditText(或其派生类)包装起来,以便在用户输入文本导致提示被隐藏时显示浮动标签。

TextInputEditText:

https://developer.android.com/reference/android/support/design/widget/TextInputEditText.html

EditText 的特殊子类,专为 TextInputLayout 的子元素而设计。


2

TextInputLayout vs TextInputEditText

TextInputLayout必须包含TextInputEditText而不是普通的EditText,因为它专门设计用于内部使用。

如果您在TextInputLayout中添加EditText而不是TextInputEditText,则会收到警告:

EditText added is not a TextInputEditText. Please switch to using that class instead.

例如,如果您在横屏模式下包装EditText,则会得到一个大框,但提示信息没有显示。
TextInputLayout具有浮动提示、错误标签、字符计数器、密码可见性、动画以及它们的自定义功能。

1
我没有看到这个警告。 - Sam Chen

1

TextInputLayoutTextInputEditText是不同的。如官方文档中这里所述,TextInputLayoutTextInputEditText应该像下面的官方示例一样使用。

<android.support.design.widget.TextInputLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content">

<android.support.design.widget.TextInputEditText
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="@string/form_username"/>

</android.support.design.widget.TextInputLayout>

此外,当你比较TextInputEditTextEditText时,主要的区别在于TextInputEditText在横屏模式下提供了一个提示。这在TWiStErRob中有详细解释。希望这回答了你的问题。谢谢。

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