【问题标题】:Always make the prefix text in text input layout visible like the hint始终使文本输入布局中的前缀文本像提示一样可见
【发布时间】:2021-10-25 10:47:41
【问题描述】:

我在文本输入布局中有一个前缀文本,但它仅在我单击文本输入编辑文本内时显示,该文本输入布局位于具有我的前缀文本的文本输入布局内。如何使前缀文本始终与提示一起显示

下面是我的文本输入编辑文本


<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/til_number"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/til_country"
                android:layout_marginStart="30dp"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginBottom="10dp"
                android:padding="5dp"
                app:boxBackgroundColor="#EFEFF2"
                app:prefixText="+01&#160;-&#160;"
                app:shapeAppearanceOverlay="@style/RoundedTextInputLayout">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/et_first_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:hint="@string/number"
                    android:inputType="phone"
                    android:maxLength="100"
                    android:textSize="15sp" />

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


我想要实现的是

+01&#160;-&#160;

前缀中的上述文本应始终显示

我已尝试执行以下操作


app:prefixTextColor="@color/secondaryDarkColor"

【问题讨论】:

    标签: android-studio prefix android-textinputlayout textinputlayout


    【解决方案1】:

    Material.io 网站,您可以使用:

    向文本字段添加前缀/后缀:

    app:prefixText="@string/prefix"

    在您的 com.google.android.material.textfield.TextInputLayout 中。

    Android Studio 预览将在文本视图中不显示前缀,但如果您在设备上运行应用程序,当用户选择输入文本时,将显示前缀。下面是我测试的截图。

    • 未选择输入布局

    • 选择输入布局(my_prefix,如果我在 xml 中用作文本前缀,typed text 是我从键盘添加的)

    【讨论】:

    • 我已经尝试删除 app:boxBackgroundColor="#EFEFF2" 但它仍然没有显示
    【解决方案2】:

    如果你检查视图的逻辑,你会看到前缀文本将在以下情况下被隐藏:

    prefixTextView.setVisibility((prefixText != null && !isHintExpanded()) ?可见:消失);

    app:expandedHintEnabled="false" 添加到您的 TextInputLayout 中,您的前缀文本将保持可见:

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/text_input_layout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            app:expandedHintEnabled="false"
            app:prefixText="MyPrefix: ">
    
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/text_input_edit_text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="My optional Hint" />
    
        </com.google.android.material.textfield.TextInputLayout>
    

    注意:当然提示会像这样显示在顶部。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      • 2011-10-16
      相关资源
      最近更新 更多