How to change the default disabled EditText's style?

偶尔善良 提交于 2019-12-20 09:18:10

问题


It isn't looking nice when using EditText enabled="false". How can I change it automatically for all of the controls?

I've added this image for reference.

Can someone help me? Thanks.


回答1:


In the color file define your color:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@color/disabled_color" />
    <item android:color="@color/normal_color"/>
</selector>

In the layout:

<EditText
    android:text="whatever text you want"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/example" />
</EditText>



回答2:


Create a custom selector in drawables and set it as

<EditText android:textColor="@drawable/edit_text_selector" />

1) in 'colors.xml' define the color for enabled and disabled state:

<color name="enabled_color">#F7FE2E</color>
<color name="disabled_color">#000000</color>

2) in 'drawable/edit_text_selector.xml':

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@color/disabled_color"/>
    <item android:color="@color/normal_color"/>
</selector>

3) in your layout.xml:

<EditText
    android:id="@+id/customEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:enabled="false"
    android:text="Hello!"
    android:textColor="@drawable/edit_text_selector" />


来源:https://stackoverflow.com/questions/16337063/how-to-change-the-default-disabled-edittexts-style

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