EditText背景基线颜色根据其在android中的焦点而变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25661788/
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 09:37:44  来源:igfitidea点击:

EditText background base line color changing based on its focus in android

androidcolorsbackgroundandroid-edittext

提问by koti

I need to design EditText with base line showed in below image and it will be change to some other color when it receives focus.!

我需要用下图所示的基线设计 EditText,当它收到焦点时它会变成其他颜色。!

enter image description here

在此处输入图片说明

i am using the following.

我正在使用以下内容。

 <EditText    
    android:id="@+id/mobilenumber" 
    android:drawableLeft="@drawable/mobile"
    android:drawablePadding="15dp" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:background="@drawable/edt_bg"
    android:singleLine="true"
    android:textColorHint="#FFFFFF"
    android:hint="@string/mobilenumber"
    android:layout_marginTop="20dp"
    android:layout_gravity="center"
    android:padding="5dp"
    android:imeOptions="actionNext"
    android:maxLength="10"
    android:textColor="#ffffff"
    android:textCursorDrawable="@null"

    />

So, please guide me how to handle this.

所以,请指导我如何处理这个问题。

回答by jobi mg

You can add theme for EditText

您可以为 EditText 添加主题

<style name="EditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorControlNormal">@color/primary</item>
    <item name="colorControlActivated">@color/primary</item>
    <item name="colorControlHighlight">@color/primary</item>
</style>

And you can use in EditText as

你可以在 EditText 中使用

<EditText
    android:id="@+id/edtCode"
    android:layout_width="match_parent"
    android:layout_height="40dp"
   .......................
    android:theme="@style/EditTextTheme">

</EditText>

回答by Slashbin

use this in your editText style

在您的 editText 样式中使用它

<item name="backgroundTint">@color/focus_tint_list</item>

focus_tint_list.xml

focus_tint_list.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true" android:color="?attr/colorAccent"/>

   <item android:state_focused="false" android:color="#999999"/>

</selector>

回答by SilentKiller

Try Holo Color generator for custom colors.

尝试自定义颜色的 Holo Color 生成器。

Android Holo Colors

Android 全息颜色

You can create Selector and set it as background of the EditText

您可以创建 Selector 并将其设置为背景 EditText

res/drawable/edit_text.xml

res/drawable/edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_window_focused="false"
        android:state_enabled="true"
        android:drawable="@drawable/textfield_default" />

    <item
        android:state_window_focused="false"
        android:state_enabled="false"
        android:drawable="@drawable/textfield_disabled" />

    <item
        android:state_pressed="true"
        android:drawable="@drawable/textfield_pressed" />

    <item
        android:state_enabled="true"
        android:state_focused="true"
        android:drawable="@drawable/textfield_selected" />

    <item
        android:state_enabled="true"
        android:drawable="@drawable/textfield_default" />

    <item
        android:state_focused="true"
        android:drawable="@drawable/textfield_disabled_selected" />

    <item
        android:drawable="@drawable/textfield_disabled" />

</selector>

layout :

布局 :

<EditText    
    android:id="@+id/mobilenumber"
    ....
    android:background="@drawable/edit_text" />

回答by Juanes30

I needed the same thing, and I solved it in the following way:

我需要同样的东西,我通过以下方式解决了它:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_focused="true" android:state_window_focused="true">
   <shape>
     <stroke android:color="@color/blue_500" android:width="2dp" />
     <corners android:radius="45dp"/>
   </shape>
</item>
<item android:state_focused="false" android:state_window_focused="false">
  <shape>
    <stroke android:color="@color/grey_600" android:width="2dp"/>
    <corners android:radius="45dp"/>
  </shape>
</item>
</selector>

回答by Anuragraphy

For changing the baseline of EditText when it is on focus you can simply go to the colors.xml file in your project and change the "colorAccent" value.

要在获得焦点时更改 EditText 的基线,您只需转到项目中的 colors.xml 文件并更改“colorAccent”值。

res/values/colors.xml

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="colorAccent">#DESIRED_COLOR</color>
</resources>`