当使用TextInputLayout时更改EditText的textAllCaps属性

6
我正在使用设计库中的新TextInputLayout。我成功地让它显示并更改了浮动标签的颜色。不幸的是,它没有将文本转换为大写字母。
我不想更改strings.xml文件,因为我只想让这个标签变成大写字母形式。我已尝试在布局、样式和程序上更改textAllCaps,但EditText提示的大小写总是与strings.xml相同。
以下是我的TextInputLayout和EditText的XML代码:
<android.support.design.widget.TextInputLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android.support.design:hintTextAppearance="@style/TextInputLayout">


<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/city"
    android:textColorHint="@color/black"
    android:hint="@string/city" />

</android.support.design.widget.TextInputLayout>

以下是我使用的TextInputLayout样式(我尝试使用仅有的"textAllCaps"属性(而不带"android:")但对我没有任何效果):

<style name="TextInputLayout" parent="@android:style/TextAppearance">
 <item name="android:textAllCaps">true</item>
</style>
2个回答

2
解决方案与@Janvi几乎相同,但是它是通过BindingAdapter解决的,可以在xml中使用而不是代码:
@BindingAdapter("android:hint")
fun setTextInputLayoutHint(textInputLayout: TextInputLayout, hint: CharSequence?) {
    textInputLayout.hint = hint?.toString()?.toUpperCase()
}

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_first_name"
        android:hint="@{@string/first_name}"

enter image description here

PS:
提示应该设置在TextInputLayout上,而不是TextInputEditText或EditText上。 https://material.io/develop/android/components/text-input-layout/

0

在你的 .xml 文件中

<android.support.design.widget.TextInputLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android.support.design:hintTextAppearance="@style/TextInputLayout">


<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/city"
    android:textColorHint="@color/black"
    android:hint="@string/city" />

</android.support.design.widget.TextInputLayout>

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