【问题标题】:Change EditText textAllCaps when using TextInputLayout使用 TextInputLayout 时更改 EditText textAllCaps
【发布时间】:2018-10-12 20:52:41
【问题描述】:

我正在使用设计库中的新 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>

【问题讨论】:

    标签: android


    【解决方案1】:

    在您的 .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>
    

    【讨论】:

      【解决方案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}"
      

      PS:

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

      【讨论】:

        猜你喜欢
        • 2015-08-29
        • 2018-12-29
        • 2015-04-23
        • 1970-01-01
        • 2015-10-23
        • 2021-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多