How to change the border color(un-focused) of an EditText?

后端 未结 6 1946
[愿得一人]
[愿得一人] 2020-12-02 20:05

I changed the background color of the EditText to transperant. Now the EditText looks invisible when not focused. So how can I change the un-focused border color of EditText

相关标签:
6条回答
  • 2020-12-02 20:30

    You can create a linearlayout with background color the color which you want the border should be. Then place the Edit text inside this Linearlayout and give some background color. Eg : if border is grey then background for Linearview is grey. and edit text background black.

    for width of border you can give padding = "1dp" for border_width = "1dp".

    0 讨论(0)
  • 2020-12-02 20:35

    You can use attr app:backgroundTint if you only want to change the EditText border color:

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:backgroundTint="#000"
    /> 
    
    0 讨论(0)
  • 2020-12-02 20:48

    Create a XML file with the following in drawable (say backwithborder.xml):

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#00000000" />
        <stroke android:width="1dip" android:color="#ffffff" />
    </shape>
    

    and for the EditText user attribute android:background="@drawable/backwithborder"

    0 讨论(0)
  • 2020-12-02 20:50

    It's so late for answered but I wrote this code for new developers:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <stroke android:width="2dp" android:color="@color/components_colorPrimaryDark" />
                    <solid android:color="@color/components_white" />
                </shape>
            </item>
    
            <item android:bottom="2dp">
                <shape android:shape="rectangle">
                    <solid android:color="@color/components_white" />
                </shape>
            </item>
        </layer-list>
    </item>
    <item android:state_focused="false">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <stroke android:width="1dp" android:color="@color/components_colorAccent" />
                    <solid android:color="@color/components_white" />
                </shape>
            </item>
    
            <item android:bottom="1dp">
                <shape android:shape="rectangle">
                    <solid android:color="@color/components_white" />
                </shape>
            </item>
        </layer-list>
    </item>
    

    0 讨论(0)
  • 2020-12-02 20:55

    To set the background of edittext to transparent you can use following :

    android:background="@null"
    

    See following links

    Android EditText Transparent Background

    setting the background attribute to transparent in editext dynamically

    Overriding onDraw() for an EditText widget with a blank implementation has no effect

    http://www.androidworks.com/changing-the-android-edittext-ui-widget

    0 讨论(0)
  • 2020-12-02 20:57

    Android OS itself adds border to EditText when user focus on it. The color depends on the OS version. Sometimes we might want to get rid of default focus border and there is a way to do it.

    We can keep the background color as transparent to remove the EditText border on focus.

         <EditText 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:background="#00000000"
          />
    
    0 讨论(0)
提交回复
热议问题