如何在Android中将Textinputlayout提示文字显示为多行?

14

我尝试了许多网上提供的选项,但似乎没有一个有效。

我已经使用了这个XML来达到这个目的。

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintTextAppearance="@style/TextLabel">

    <com.projects.fonts.RobotoEditText
        style="@style/EditText"
        android:hint="@string/description"
        android:singleLine="false"
        app:font="@string/roboto_regular" />

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

我还设置了

<item name="android:singleLine">false</item>

在TextLabel和EditText中都尝试过,但是都没有起作用。


RobotoEditText是否扩展了android.support.design.widget.TextInputEditText? - qnd-S
@qnd-S 是的,它确实如此。 - Vaibhav Jaiswal
3个回答

12

你无法做到。

TextInputLayout 使用 CollapsingTextLayout 来测量提示文本,它将其作为一个 CharSequence 进行测量:

mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length())

CharSequence没有行的概念,因此您无法添加更多行。


如果提示太长,那就不算是提示了。 如果仍然想要显示它,请考虑在下方/上方/覆盖上添加一个单独的TextView并进行动画处理(如果需要)。


3
你可以使用TextInputLayout和TextInputEditText来实现这一点。
在TextInputEditText中,设置提示文本(android:hint =“提示文本”)。
在TextInputLayout中,必须通过设置“app:hintEnabled =”false“来禁用提示。
注意:
如果将hintEnabled保留为true(默认值为true),则提示将为单行,并在用户点击EditText以使其获得焦点时变成标签。
如果将hintEnabled更改为false,则此功能将被删除,并且EditText将按预期显示多行提示。它不会变成标签,并且一旦用户开始实际输入,提示就会消失。
以下是示例代码:
<com.google.android.material.textfield.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:hintEnabled="false"
                    app:boxBackgroundMode="none">

                    <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Your long hint"/>
                </com.google.android.material.textfield.TextInputLayout>

0
simas的回答相反,似乎有一种解决方法。您可以通过在TextInputEdittext中以编程方式设置提示来实现预期的行为。不过,这样做后,浮动标签将无法正常工作,但我认为对于如此长的提示,我们并不期望它能正常工作。
重要提示:如果我们将提示设置为TextInputLayout,则它将无法正常工作。

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