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">
        
        "android:textColorHint">@color/warm_gray
        "colorControlNormal">@color/warm_gray
        "colorControlActivated">@color/colorAccent
    style>

可以得到想要的结果
EditText设置下划线、提示字颜色、输入字体颜色及问题_第1张图片

显示错误设置

测试时,把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" />

@color/ic_white 18sp

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

<style name="MyEditTextStyle">
    
    "android:textColor">@color/ic_white
    "android:textSize">18sp
    "android:textColorHint">@color/warm_gray
    "colorControlNormal">@color/warm_gray
    "colorControlActivated">@color/colorAccent
style>

结果显示,error的字体设置也变化了,输入的字体也没显示为白色, 而是黑色,错误提示字体也很大
EditText设置下划线、提示字颜色、输入字体颜色及问题_第2张图片

总结

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

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

附颜色


<resources>
    <color name="colorPrimary">#0A1026color>
    <color name="colorPrimaryDark">#0A1026color>
    <color name="colorAccent">#F57F17color>
    <color name="color_purl">#7e53c5color>
    <color name="white">#EAE9E9color>
    <color name="ic_white">#FFF3E0color>
    <color name="tmn_white">#F6F5F5color>
    <color name="line_gray">#E6E6E6color>
    <color name="warm_gray">#7C7B7Bcolor>
    <color name="peal_white">#FEFDFDcolor>
    <color name="warm_red">#E5514Ecolor>
    <color name="warm_blue">#42A5F5color>
    <color name="light_blue">#EC6BBAFAcolor>
resources>

你可能感兴趣的:(Android,问题,随笔,android)