如何使用框来制作一个文本输入布局?

4

像这样:enter image description here

当获得焦点时,单词电子邮件或电话是提示,会跳转到顶部。我尝试搜索库,甚至查看文本输入布局的代码,但无法理解如何实现这样的效果???

1个回答

13
要获得那种一般的外观,您只需将“outline”样式应用于TextInputLayout标记即可:

要获得那种一般的外观,您只需将“outline”样式应用于TextInputLayout标记即可:

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    ...>

    <com.google.android.material.textfield.TextInputEditText
        .../>

</com.google.android.material.textfield.TextInputLayout

“电子邮件或电话”文本跳转到顶部是布局上设置的“提示”文本。

<com.google.android.material.textfield.TextInputLayout
    android:hint="Email or phone"
    ...>

TextInputLayout的开发者指南:https://material.io/develop/android/components/text-input-layout/

你需要在你的应用程序中使用Google Material Components库。入门指南:https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md


这个完美地运行了!谢谢。另外,对于其他读者来说,如果你的应用程序具有 targetSdkVersion 28'com.android.support:design:28.0.0' 库,则可能不需要设计支持库。那么这个 TextInputLayout 已经存在于您的应用程序中了。上面的链接是进一步了解的好资源。 - user4831795

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