java - 如何更改 EditText 的底线颜色?

标签 java android android-edittext

我正在尝试更改 EditText 下底线的颜色。我看不到像我这样的关于 SO 的问题,但没有任何帮助。这是我的 xml:

<EditText
        android:id="@+id/loginEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"         
        android:hint="@string/login"
         android:theme="@style/EditTextStyle"
        android:inputType="textPersonName" >

这是我的风格:

 <resources>
    <style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
       <item name="colorPrimary">@color/material_blue_500</item>
       <item name="colorPrimaryDark">@color/material_blue_700</item>
       <item name="colorAccent">@color/material_blue_700</item>  
       <item name="colorControlNormal">@color/material_blue_700</item>
       <!-- Active thumb color & Active track color(30% transparency) -->
       <item name="colorControlActivated">@color/material_blue_500</item>
       <!-- Inactive thumb color -->
       <item name="colorSwitchThumbNormal">@color/lightGrey</item>
       <!-- Inactive track color(30% transparency) -->
       <item name="android:colorForeground">@color/grey</item>   
       <item name="android:editTextStyle">@style/EditTextStyle</item>
       <item name="colorControlNormal">@color/material_blue_400</item>
       <item name="colorControlActivated">@color/material_blue_700</item>
       <item name="colorControlHighlight">@color/material_blue_700</item>

    </style>

    <style name="EditTextStyle" parent="Widget.AppCompat.EditText">
             <item name="colorControlNormal">@color/material_blue_400</item>
             <item name="colorControlActivated">@color/material_blue_700</item>
             <item name="colorControlHighlight">@color/material_blue_700</item>
    </style>
    </resources> 

这是我的颜色:

<color name="green">#00FF00</color>
<color name="thumbColor">#66aaaaaa</color>
<color name="darkGrey">#888</color>
<color name="grey">#ccc</color>
<color name="white">#fff</color>
<color name="lightGrey">#bbb</color>
<color name="veryMidGrey">#eeeeee</color>
<color name="veryLightGrey">#efefef</color>
<color name="transparent">#00000000</color>
<color name="whiteTransparent">#CCFFFFFF</color>
<color name="blackTransparent">#CC000000</color>
<color name="material_blue_800">#1565C0</color>
<color name="material_blue_500">#2196F3</color>
<color name="material_blue_400">#42A5F5</color>
<color name="material_blue_700">#1976D2</color>
<color name="material_blue_300"> #64B5F6</color>
<color name="black">#000</color>

但我的底线还是黑的。

enter image description here

做什么?

最佳答案

使用:

editText.getBackground().mutate().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_ATOP);

注意:getColor() 方法在 API 级别 23 中已弃用。因此,您需要使用 Contextcompat 来获取颜色。

关于java - 如何更改 EditText 的底线颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37322790/

相关文章:

Java HtmlCleaner : Does not handle extended ascii characters

java.lang.RuntimeException : Unable to instantiate activity ComponentInfo java. lang.NullPointerException

android - Jetpack Compose 的方向

android - Context.getPackageName() 与 BuildConfig.APPLICATION_ID

Android:过滤特定 Uri 模式的 Intent

android - 如何设计没有框和下划线的自定义EditText?

android - 按 EditText 时的 TimePicker 对话框

java - 服务器端 Java 代码中要链接的字符串

java - 您能解释一下计算哈希码的所谓 Java 标准方法吗?

java - 对话框后请求 EditText 时不显示键盘