Android 文本属性下方的 EditText 下划线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21480398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:38:52  来源:igfitidea点击:

EditText underline below text property

androidloginandroid-edittext

提问by AndroidEnthusiast

I would like to change the blue colour below the edit text, i don't know what property it is.

我想更改编辑文本下方的蓝色,我不知道它是什么属性。

I tried using a different background colour for it but it didn't work.

我尝试为它使用不同的背景颜色,但没有用。

I've attached an image below:

我在下面附上了一张图片:

enter image description here

在此处输入图片说明

回答by Jing Li

It's actually fairly easy to set the underline color of an EditText programmatically (just one line of code).

以编程方式设置 EditText 的下划线颜色实际上相当容易(只需一行代码)。

To set the color:

要设置颜色:

editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);

To remove the color:

去除颜色:

editText.getBackground().clearColorFilter();

Note: when the EditText has focuson, the color you set won't take effect, instead, it has a focus color.

注意:当 EditText 有焦点时,你设置的颜色不会生效,而是有焦点颜色。

API Reference:

API参考:

Drawable#setColorFilter

可绘制#setColorFilter

Drawable#clearColorFilter

可绘制#clearColorFilter

回答by Soheil Setayeshi

Use android:backgroundTint=""in your EditTextxml layout.

使用android:backgroundTint=""你的EditTextXML布局。

For api<21 you can use AppCompatEditTextfrom support library thenapp:backgroundTint=""

对于 api<21,您可以AppCompatEditText从支持库中使用app:backgroundTint=""

回答by Akariuz

You have to use a different background image, not color, for each state of the EditText(focus, enabled, activated).

对于EditText(焦点、启用、激活)的每个状态,您必须使用不同的背景图像,而不是颜色。

http://android-holo-colors.com/

http://android-holo-colors.com/

In the site above, you can get images from a lot of components in the Holo theme. Just select "EditText" and the color you want. You can see a preview at the bottom of the page.

在上面的站点中,您可以从 Holo 主题的许多组件中获取图像。只需选择“EditText”和您想要的颜色。您可以在页面底部看到预览。

Download the .zip file, and copy paste the resources in your project (images and the XML).

下载 .zip 文件,并将资源(图像和 XML)复制粘贴到您的项目中。

if your XML is named: apptheme_edit_text_holo_light.xml (or something similar):

如果您的 XML 被命名为: apptheme_edit_text_holo_light.xml (或类似的东西):

  1. Go to your XML "styles.xml" and add the custom EditTextstyle:

    <style name="EditTextCustomHolo" parent="android:Widget.EditText">
       <item name="android:background">@drawable/apptheme_edit_text_holo_light</item>
       <item name="android:textColor">#ffffff</item>
    </style>
    
  2. Just do this in your EditText:

    <EditText
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       style="@style/EditTextCustomHolo"/>
    
  1. 转到您的 XML "styles.xml" 并添加自定义EditText样式:

    <style name="EditTextCustomHolo" parent="android:Widget.EditText">
       <item name="android:background">@drawable/apptheme_edit_text_holo_light</item>
       <item name="android:textColor">#ffffff</item>
    </style>
    
  2. 只需在您的EditText

    <EditText
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       style="@style/EditTextCustomHolo"/>
    

And that's it, I hope it helps you.

就是这样,我希望它可以帮助你。

回答by Sufian

This works fine for old and new version of Android (works fine even on API 10!).

这适用于新旧版本的 Android(即使在 API 10 上也能正常工作!)。

Define this style in your styles.xml:

在您的styles.xml:

<style name="EditText.Login" parent="Widget.AppCompat.EditText">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textColorHint">@android:color/darker_gray</item>
    <item name="colorAccent">@color/blue</item>
    <item name="colorControlNormal">@color/blue</item>
    <item name="colorControlActivated">@color/blue</item>
</style>

And now in your XML, set this as theme and style (styleto set textColor, and themeto set all other things):

现在在您的 XML 中,将其设置为主题和样式(样式设置textColor主题设置所有其他内容):

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    style="@style/EditText.Login"
    android:theme="@style/EditText.Login"/>


Edit

编辑

This solution causes a tiny UI glitch on newer Android versions (Lollipop or Marshmallow onwards) that the selection handles are underlined.

此解决方案会导致在较新的 Android 版本(棒棒糖或棉花糖以后)上出现微小的 UI 故障,其中选择句柄带有下划线。

This issue is discussed in this thread. (I haven't tried this solution personally)

这个问题在这个线程中讨论。(我没有亲自尝试过这个解决方案)

回答by Dharani Kumar

you can change Underlineof EditTextcolor specifying it in styles.xml. In your app theme styles.xml add the following.

您可以更改EditText颜色的下划线,在styles.xml 中指定它。在您的应用主题styles.xml 中添加以下内容。

<item name="android:textColorSecondary">@color/primary_text_color</item> 

As pointed out by ana in comment section

正如 ana 在评论部分指出的那样

  <item name="android:colorControlActivated">@color/black</item>

setting this in theme style works well for changing color of an edittext underline.

在主题样式中设置它可以很好地更改编辑文本下划线的颜色。

回答by Vulovic Vukasin

So, you need to create a new .xml file in your drawable folder.

因此,您需要在 drawable 文件夹中创建一个新的 .xml 文件。

In that file paste this code:

在该文件中粘贴此代码:

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

And in your EditText, set

在你的 EditText 中,设置

android:background="@drawable/your_drawable"

You can play with your drawable xml, set corners, paddings, etc.

您可以使用可绘制的 xml、设置角、填充等。

回答by Roberto

In your app style define the property colorAccent. Here you find an example

在您的应用程序样式中定义属性colorAccent。在这里你可以找到一个例子

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/action_bar</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/action_bar</item>
</style>

回答by Matin Ashtiani

You can change the color of EditText programmatically just using this line of code easily:

您可以轻松地使用这行代码以编程方式更改 EditText 的颜色:

edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));

edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));

回答by SAndroidD

To change bottom line color, you can use this in your app theme:

要更改底线颜色,您可以在应用主题中使用它:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="colorControlNormal">#c5c5c5</item>
    <item name="colorControlActivated">#ffe100</item>
    <item name="colorControlHighlight">#ffe100</item>
</style>

To change floating label color write following theme:

要更改浮动标签颜色,请编写以下主题:

<style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
<item name="android:textColor">#4ffd04[![enter image description here][1]][1]</item>
</style>

and use this theme in your layout:

并在您的布局中使用此主题:

 <android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout">

    <EditText
        android:id="@+id/edtTxtFirstName_CompleteProfileOneActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:capitalize="characters"
        android:hint="User Name"
        android:imeOptions="actionNext"
        android:inputType="text"
        android:singleLine="true"
        android:textColor="@android:color/white" />

</android.support.design.widget.TextInputLayout>

enter image description here

在此处输入图片说明

回答by Gogi Bobina

You can do it with AppCompatEditText and color selector:

您可以使用 AppCompatEditText 和颜色选择器来实现:

            <androidx.appcompat.widget.AppCompatEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:backgroundTint="@color/selector_edittext_underline" />

selector_edittext_underline.xml:

selector_edittext_underline.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/focused_color" 
          android:state_focused="true" />
    <item android:color="@color/hint_color" />
</selector>