Android:如何默认显示TextInputLayout的后缀文本?

3
我正在使用最新版本的 Android Material Design 库 (1.2.0-alpha05)。我已经在 TextInputLayout 中添加了 app:suffixText="PlaceHolder"。我希望默认情况下显示 suffixText,但目前只有在 TextInputLayout 获得焦点时才可见。 当 TextInputLayout 失去焦点时,我无法看到 suffixText。以下是我的代码:

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:suffixText="PlaceHolder"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:hint="hint">

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

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

When I am launching the Activity, I can't see suffix text of TextInputLayout

Only after clicking on TextInputEditText, I can see suffix text

请告诉我,如何默认显示后缀文本。

为此创建自定义视图。 - Faisal Naseer
3个回答

7
你可以使用 app:expandedHintEnabled="false" 来在文本框为空时显示前缀/后缀。
        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:suffixText="...."
            app:expandedHintEnabled="false"
            >

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

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

enter image description here


1

我刚刚用 Kotlin 创建了一个扩展:

fun TextInputLayout.keepSuffixVisible() {
    val suffixParent = this.suffixTextView.parent as View
    suffixParent.viewTreeObserver.addOnGlobalLayoutListener {
        if (suffixParent.visibility != View.VISIBLE) {
            suffixParent.visibility = View.VISIBLE
        }
    }
    this.suffixTextView.viewTreeObserver.addOnGlobalLayoutListener {
        if (this.suffixTextView.visibility != View.VISIBLE) {
            this.suffixTextView.visibility = View.VISIBLE
        }
    }
    this.suffixTextView.visibility = View.VISIBLE
}

0

在代码中

inputLayout.suffixTextView.visibility = View.VISIBLE
inputLayout.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
    inputLayout.suffixTextView.visibility = View.VISIBLE
}

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