【问题标题】:How to change bottom line color of an edit text?如何更改编辑文本的底线颜色?
【发布时间】:2016-05-24 10:47:05
【问题描述】:

我有一个对话框布局,其中我有 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后得到的。

谢谢你..

【问题讨论】:

  • 您可以参考我之前的answer 并根据需要删除dashgap或stroke。
  • 但是为什么会这样呢?为什么只用于第一个编辑文本和其他两个?如果我不想使用drawable? @SathishKumarJ
  • 但是为什么会这样呢?为什么只用于第一个编辑文本和其他两个?如果我不想使用drawable? @ShreeKrishna
  • @user6265109 试试我的回答并告诉我状态

标签: android colors android-edittext


【解决方案1】:

它只是在你的应用主题定义中覆盖 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);

【讨论】:

  • 我已经尝试过上述两种方法。 @user2934536
【解决方案2】:

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

【讨论】:

  • 我的颜色口音是粉红色的。它显示在两个编辑文本上,但不在第一个上。 @Naitik
【解决方案3】:

在可绘制文件夹中创建textfield_bg_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时,我将颜色声明为强调色,但它仍然向我显示绿色。我的强调色是粉红色。令人沮丧:-(@Dhaval Patel
  • 您是否在 EditText 或 Style 中设置了 drawable android:background="@drawable/textfield_bg_drawable"
  • 尝试清理并重建。
  • 试过了。也做了无效的缓存和重新启动。 @Dhaval Patel
  • 请检查添加的有问题的屏幕截图。对于两个编辑文本,我都使用相同的可绘制对象。他们仍然是不同的。 @Dhaval Patel
【解决方案4】:

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

        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

【讨论】:

  • @user6265109 实际上它在我这边工作你有什么错误吗??
  • @user6265109 请查看此链接android--code.blogspot.in/2015/08/…
  • 这有什么问题?当我使用这个drawable时,我将颜色声明为强调色,但它仍然向我显示绿色。我的强调色是粉红色。令人沮丧的@Dhiraj
  • @user6265109 当你尝试使用我的代码时得到了什么输出
  • 它将完整的编辑文本显示为绿色,尽管我已经给出了颜色强调,即粉红色。它不会将颜色设置为底线。这也是第一个编辑文本的问题,它只显示绿色,我尝试使用两个编辑文本作为第二个编辑文本,它显示底线为粉红色但不是第一个?怎么了? :-( @Dhiraj
【解决方案5】:

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

在你的editText中添加这一行

android:focusable="true"

【讨论】:

    【解决方案6】:

    您需要在活动打开时从您的第一个编辑文本中禁用焦点,因此请设置 android:requestFocus= "false"

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

    edittext.clearFocus();

    在您的 java 文件中。

    它不会选择你的原色。

    希望它有效!

    【讨论】:

      猜你喜欢
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多