簡體   English   中英

如何在多行中獲取 Textinputlayout 提示 android?

[英]How to get Textinputlayout hint android in multiple lines?

我嘗試了許多在線可用的選項,但它們似乎都不起作用。

為此,我使用了這個 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 中。 但他們都沒有工作。

你不能。

TextInputLayout使用CollapsingTextLayout將提示測量為CharSequence

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

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


如果它這么長,這不是一個提示。 如果您仍然想顯示它,請考慮在下方/上方/上方添加一個單獨的TextView並為其設置動畫(如果想要/需要)。

simas答案相反,似乎有一種解決方法。 您可以通過在 TextInputEdittext 中以編程方式設置提示來實現預期行為。 下坡是浮動標簽在那之后不起作用,我相信這不是我們期望的那么長的提示。

重要提示:如果我們將提示設置為 TextInputLayout,那么它將不起作用。

您可以使用 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM