EditText设置下划线、提示字颜色、输入字体颜色及问题

显示正确设置

想要设置EditText的下划线、提示字颜色、输入字体颜色,可以通过设置xml属性android:theme="@style/MyEditTextStyle",例如

    <EditText
        android:id="@+id/password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="24dp"
        android:hint="@string/prompt_password"
        android:imeOptions="actionDone"
        android:inputType="textPassword"
        android:selectAllOnFocus="true"
        android:theme="@style/MyEditTextStyle"
        android:textColor="@color/ic_white"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/username" />

res/styles.xml

    <style name="MyEditTextStyle">
        <!--文本的颜色,textColor和textSize不要放到style中-->
        <item name="android:textColorHint">@color/warm_gray</item>
        <item name="colorControlNormal">@color/warm_gray</item>
        <item name="colorControlActivated">@color/colorAccent</item>
    </style>

可以得到想要的结果

显示错误设置

测试时,把textColor和textSize要放到style中,

<EditText
    android:id="@+id/password"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="24dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="24dp"
    android:hint="@string/prompt_password"
    android:imeOptions="actionDone"
    android:inputType="textPassword"
    android:selectAllOnFocus="true"
    android:theme="@style/MyEditTextStyle"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/username" />

<item name="android:textColor">@color/ic_white</item> <item name="android:textSize">18sp</item>

这两个属性一并放到style/MyEditTextStyle

<style name="MyEditTextStyle">
    <!--文本的颜色-->
    <item name="android:textColor">@color/ic_white</item>
    <item name="android:textSize">18sp</item>
    <item name="android:textColorHint">@color/warm_gray</item>
    <item name="colorControlNormal">@color/warm_gray</item>
    <item name="colorControlActivated">@color/colorAccent</item>
</style>

结果显示,error的字体设置也变化了,输入的字体也没显示为白色, 而是黑色,错误提示字体也很大

总结

设置EditText的下划线、提示字颜色、输入字体颜色,可以通过设置xml属性android:theme="@style/MyEditTextStyle"

但是不要把textColor和textSize要放到style中, 会使输入显示的字体颜色不正确,error的大小也不正确。

附颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#0A1026</color>
    <color name="colorPrimaryDark">#0A1026</color>
    <color name="colorAccent">#F57F17</color>
    <color name="color_purl">#7e53c5</color>
    <color name="white">#EAE9E9</color>
    <color name="ic_white">#FFF3E0</color>
    <color name="tmn_white">#F6F5F5</color>
    <color name="line_gray">#E6E6E6</color>
    <color name="warm_gray">#7C7B7B</color>
    <color name="peal_white">#FEFDFD</color>
    <color name="warm_red">#E5514E</color>
    <color name="warm_blue">#42A5F5</color>
    <color name="light_blue">#EC6BBAFA</color>
</resources>
  • 7
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值