2014-09-04 11 views
8

Tôi cần thiết kế EditText với đường cơ sở được hiển thị bên dưới hình ảnh và nó sẽ thay đổi thành màu khác khi nó nhận tiêu điểm.!Màu nền của đường cơ sở EditText thay đổi dựa trên tiêu điểm của nó trong android

enter image description here

tôi đang sử dụng như sau.

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

    /> 

Vì vậy, hãy hướng dẫn tôi cách xử lý việc này.

+0

đọc về thẻ StateListDrawable trong xml – pskink

+0

Bạn có thể hiểu rõ về điều này không? – koti

Trả lời

3

Thử trình tạo màu Holo cho các màu tùy chỉnh.

Android Holo Colors

Bạn có thể tạo Selector và thiết lập nó như là nền của res EditText

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

bố trí:

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

Câu trả lời hay. Mã XML của bạn đã bỏ lỡ thẻ. Khác hơn thế - cảm ơn bro! –

+1

@AlexeyFShevelyov câu trả lời được cập nhật. cảm ơn. :) – SilentKiller

25

Bạn có thể thêm chủ đề cho 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> 

Và bạn có thể sử dụng trong EditText như

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

</EditText> 
+2

Chính xác những gì cần thiết. Hoạt động hoàn hảo. Cảm ơn nhiều!!! – DmitryKanunnikoff

0

này sử dụng trong phong cách của bạn 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> 
Các vấn đề liên quan