android - 如何更改编辑文本的底线颜色?

标签 android colors android-edittext

我有一个对话框布局,其中有 3 个编辑文本,对于第一个编辑文本,它显示下划线颜色为绿色(原色),其他两个编辑文本显示颜色为粉红色(强调色)。

enter image description here

为什么会这样?它应该与其他两个编辑文本相同。

试图改变这个:

 name.getBackground().mutate().setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.SRC_ATOP);
                name.getBackground().setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.SRC_ATOP);

也由 SO 解决方案。但没有任何帮助。

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">

        ....
        <item name="android:editTextStyle">@style/EditTextStyle</item>
</style>

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

布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <LinearLayout android:layout_height="50dp"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Schedule"
            android:textColor="@android:color/white"
            android:textAppearance="@android:style/TextAppearance.Medium"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="20dp">


        <EditText
            android:id="@+id/edt_name"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:textColor="#000000"
            android:textSize="14sp"
            android:inputType="textCapSentences"
            android:hint="Name"
            android:cursorVisible="false"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp" />

        <EditText
            android:id="@+id/edt_dateTime"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:textColor="#000000"
            android:textSize="14sp"
            android:cursorVisible="false"
            android:inputType="textCapSentences"
            android:hint="Date and Time"
            android:layout_centerVertical="true"
            android:layout_alignLeft="@+id/edt_name"
            android:layout_alignStart="@+id/edt_name"
            android:layout_below="@+id/edt_name"
            android:layout_alignRight="@+id/edt_name"
            android:layout_alignEnd="@+id/edt_name"
            android:layout_marginTop="05dp" />

        <EditText
            android:id="@+id/edt_budget"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:textColor="#000000"
            android:textSize="14sp"
            android:cursorVisible="false"
            android:hint="Budget"
            android:layout_below="@+id/edt_dateTime"
            android:layout_alignLeft="@+id/edt_dateTime"
            android:layout_alignStart="@+id/edt_dateTime"
            android:layout_alignRight="@+id/edt_dateTime"
            android:layout_alignEnd="@+id/edt_dateTime"
            android:layout_marginTop="05dp"
            android:inputType="number" />

    </RelativeLayout>

</LinearLayout>

这是我实现 drawable 后得到的结果。

enter image description here

谢谢你..

最佳答案

它只是覆盖应用主题定义中的 colorControlActivated、colorControlHighlight 和 colorControlNormal 的值

<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorControlNormal">#c5c5c5</item>
        <item name="colorControlActivated">@color/accent</item>
        <item name="colorControlHighlight">@color/accent</item>
    </style>

或通过java

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

关于android - 如何更改编辑文本的底线颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37411351/

相关文章:

android - 通过代码(以编程方式)打开 EditTextPreference

java - 在动态edittext和textview中设置id

android - 使用 Firebase : Get notification when new message received - Android 的聊天应用

android 自动发送电子邮件

android重置/重新加载 fragment 数据

ios - 在ios中绘制彩色三角形

android TextView setText 不工作

javascript - hsv到rgb颜色转换背后的数学

qt - 如何在 QStatusBar 中获取彩色瞬时消息?

android - 如何让我的 EditText 字段显示当前网页 URL?