【问题标题】:How to get Textinputlayout hint android in multiple lines?如何在多行中获取 Textinputlayout 提示 android?
【发布时间】:2016-11-20 21:23:02
【问题描述】:

我尝试了许多在线可用的选项,但似乎没有一个有效。

我已经为此目的使用了这个 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 是的..

标签: android android-edittext hint textinputlayout


【解决方案1】:

您可以使用 TextInputLayout 和 TextInputEditText 来做到这一点。

在 TextInputEditText 中像往常一样设置提示 (android:hint="Hint Text")。

在 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>

【讨论】:

    【解决方案2】:

    simas 答案相比,似乎有一种解决方法。您可以通过在 TextInputEdittext 中以编程方式设置提示来实现预期的行为。下坡是浮动标签在那之后不起作用,我相信这不是我们期望的这么长的提示。

    重要提示:如果我们将提示设置为 TextInputLayout,那么它将不起作用。

    【讨论】:

      【解决方案3】:

      你不能。

      TextInputLayout 使用CollapsingTextLayout 将提示测量为CharSequence

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

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


      如果它很长,这不是一个暗示。 如果您仍想显示它,请考虑在下方/上方/上方添加单独的 TextView 并为其设置动画(如果需要/需要)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-25
        • 1970-01-01
        • 2020-07-02
        • 2018-11-20
        • 2018-03-15
        • 2017-04-18
        • 2021-12-11
        相关资源
        最近更新 更多