2014-09-04 61 views
8

我需要設計EditText,其基線顯示在下面的圖像中,它會在接收焦點時更改爲其他顏色。EditText背景基線顏色根據其焦點更改android

enter image description here

我使用以下。

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

    /> 

所以,請指導我如何處理這個問題。

+0

閱讀關於StateListDrawable標籤 in xml – pskink 2014-09-04 09:31:50

+0

你可以對此有一些清楚的想法嗎? – koti 2014-09-04 09:36:14

回答

3

嘗試Holo自定義顏色的顏色生成器。

Android Holo Colors

您可以創建選擇並將其設置爲EditText

資源的背景/繪製/ 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> 

佈局:

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

好的答案。您的XML代碼會丟失標記。除此之外 - 謝謝兄弟! – 2016-12-09 17:06:04

+1

@AlexeyFShevelyov答案已更新。謝謝。 :) – SilentKiller 2016-12-13 05:24:56

25

您可以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> 

而且你可以在使用的EditText作爲

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

</EditText> 
+2

確切需要什麼。完美的作品。非常感謝你!!! – DmitryKanunnikoff 2017-06-03 23:28:22

0

使用這個在您的EDITTEXT風格

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