【问题标题】:Android: How to display suffixText of TextInputLayout by default?Android:如何默认显示TextInputLayout的suffixText?
【发布时间】:2020-03-27 04:34:53
【问题描述】:

我正在使用最新版本的 android 材料设计库 (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>

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

【问题讨论】:

  • 为其创建自定义视图。

标签: android android-layout material-design android-textinputlayout android-textinputedittext


【解决方案1】:

当文本字段为空时,您也可以使用 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>

【讨论】:

    【解决方案2】:

    在代码中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 2015-02-09
      相关资源
      最近更新 更多