始终使文本输入布局中的前缀文本像提示一样可见
Always make the prefix text in text input layout visible like the hint
我在文本输入布局中有一个前缀文本,但只有当我在文本输入编辑文本中单击时才会显示,该文本输入布局位于具有我的前缀文本的文本输入布局中。我怎样才能使前缀文本始终显示提示
下面是我的文本输入编辑文本
<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 - "
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 - 
前缀中的上述文字应始终显示
我已尝试执行以下操作
app:prefixTextColor="@color/secondaryDarkColor"
从 Material.io 网站,您可以使用:
Adding a prefix/suffix to a text field:
app:prefixText="@string/prefix"
在你的com.google.android.material.textfield.TextInputLayout
.
里面
AndroidStudio 预览将不显示文本视图中的前缀,但如果您运行设备上的应用程序,当用户选择输入文本,将显示前缀。下面是我测试的截图。
未选择输入布局
选择了输入布局(my_prefix
如果我在 xml 中用作文本前缀,而 typed text
是我从键盘添加的内容)
如果您检查视图的逻辑,您会发现在以下情况下前缀文本将被隐藏:
prefixTextView.setVisibility((prefixText != null && !isHintExpanded())
? VISIBLE : GONE);
将 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>
注意:当然提示会这样显示在最上面
如果你想在提示未使用 Kotlin 扩展时显示后缀:
textInputLayout.viewTreeObserver.addOnGlobalLayoutListener {
textInputLayout.suffixTextView.visibility = View.VISIBLE
}
我在文本输入布局中有一个前缀文本,但只有当我在文本输入编辑文本中单击时才会显示,该文本输入布局位于具有我的前缀文本的文本输入布局中。我怎样才能使前缀文本始终显示提示
下面是我的文本输入编辑文本
<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 - "
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 - 
前缀中的上述文字应始终显示
我已尝试执行以下操作
app:prefixTextColor="@color/secondaryDarkColor"
从 Material.io 网站,您可以使用:
Adding a prefix/suffix to a text field:
app:prefixText="@string/prefix"
在你的com.google.android.material.textfield.TextInputLayout
.
AndroidStudio 预览将不显示文本视图中的前缀,但如果您运行设备上的应用程序,当用户选择输入文本,将显示前缀。下面是我测试的截图。
未选择输入布局
选择了输入布局(
my_prefix
如果我在 xml 中用作文本前缀,而typed text
是我从键盘添加的内容)
如果您检查视图的逻辑,您会发现在以下情况下前缀文本将被隐藏:
prefixTextView.setVisibility((prefixText != null && !isHintExpanded()) ? VISIBLE : GONE);
将 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>
注意:当然提示会这样显示在最上面
如果你想在提示未使用 Kotlin 扩展时显示后缀:
textInputLayout.viewTreeObserver.addOnGlobalLayoutListener {
textInputLayout.suffixTextView.visibility = View.VISIBLE
}