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

Android 如何更改编辑文本的底线颜色?,android,colors,android-edittext,Android,Colors,Android Edittext,我有一个对话框布局,其中我有3个编辑文本,对于第一个编辑文本,它将下划线颜色显示为绿色(原色),其他两个编辑文本显示为粉红色(颜色重音) 为什么会这样?它应该与其他两个编辑文本相同 试图通过以下方式更改此设置: name.getBackground().mutate().setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.SRC_ATOP); name.get

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

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

试图通过以下方式更改此设置:

 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后得到的


谢谢..

它只包括覆盖应用程序主题定义中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>

通过更改“colorAccent”颜色,您将更改颜色或编辑文本底线以及文本输入布局。

在drawable文件夹中创建
textfield\u bg\u drawable.xml
。并将其设置为
EditText
中的后台资源

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="2dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <selector>
            <item
                android:state_enabled="true"
                android:state_focused="true">
                <shape android:shape="rectangle">
                    <stroke
                        android:width="2dp"
                        android:color="@color/focused_line_color" />
                </shape>
            </item>
            <item android:state_enabled="true">
                <shape android:shape="rectangle">
                    <stroke
                        android:width="1dp"
                        android:color="@color/normal_line_color" />
                </shape>
            </item>
        </selector>
    </item>
</layer-list>

试着让以下代码在我这边正常工作

        Drawable drawable = editText.getBackground(); // get current EditText drawable
        drawable.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP); // change the drawable color

        if (Build.VERSION.SDK_INT > 16) {
            editText.setBackground(drawable); // set the new drawable to EditText
        } else {
            editText.setBackgroundDrawable(drawable); // use setBackgroundDrawable because setBackground required API 16
        }

其中,editText是您的editText

的id,我认为这是因为当您的警报布局出现时,您的第一个editText聚焦

在编辑文本中添加此行

android:focusable="true"

当活动打开时,您需要从first edittext中禁用焦点,因此设置
android:requestFocus=“false”

在xml文件中。或者试试下面的方法

edittext.clearFocus()

在java文件中

它不会选择你的原色


希望它能起作用

参考你可以参考我以前的文章,根据需要删除dashgap或stroke。但是为什么会发生这种情况呢?为什么只针对第一个编辑文本和其他两个编辑文本?如果我不想使用drawable@但是为什么会这样呢?为什么只针对第一个编辑文本和其他两个编辑文本?如果我不想使用drawable@ShreeKrishna@user6265109试一下我的答案,告诉我我已经试过了,如问题中所述@USER2934536我的颜色口音是粉红色的。它显示在两个编辑文本上,但不显示在第一个文本上@Naitik@user6265109实际上它在我这边工作,你有什么错误吗???@user6265109请看看这个链接有什么问题?当我使用这个绘图工具时,我将颜色表示为强调色,但它仍然显示绿色。我的口音是粉红色。令人沮丧@Dhiraj@user6265109当您尝试使用我的代码时,您得到了什么输出?尽管我已给出了颜色重音,即粉红色,但完整的编辑文本显示为绿色。它没有将颜色设置为底线。这也是第一个编辑文本的问题,只是它显示绿色,我尝试了两个编辑文本,第二个编辑文本,它显示底线为粉红色,但第一个没有?怎么了-(@dhiraj这有什么问题?当我使用这个drawable时,我说颜色是强调色,但它仍然显示绿色。我的强调色是粉红色。令人沮丧的是:-(@Dhaval PatelHave you set drawable
android:background=“@drawable/textfield\u bg\u drawable”
在EditText中还是在Style中?尝试清理并重新生成。尝试过。也进行了无效缓存并重新启动。@Dhaval Patel请检查有问题的添加屏幕截图。对于这两个编辑文本,我使用的是相同的drawable。但它们仍然不同。@Dhaval Patel
android:focusable="true"