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

社会主义新天地 提交于 2019-12-29 05:42:12

问题


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.!

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.


回答1:


You can add theme for 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
    android:id="@+id/edtCode"
    android:layout_width="match_parent"
    android:layout_height="40dp"
   .......................
    android:theme="@style/EditTextTheme">

</EditText>



回答2:


use this in your editText style

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

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>



回答3:


Try Holo Color generator for custom colors.

Android Holo Colors

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

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" />



回答4:


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>



回答5:


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.

res/values/colors.xml

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


来源:https://stackoverflow.com/questions/25661788/edittext-background-base-line-color-changing-based-on-its-focus-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!