Android edittext set border

A few days ago, I was working on a simple Android project. Since I am a novice, I don't know why in many places.
I encountered a problem: when I set one Edittext, its style was only a line on the bottom border. After continuous Baidu (because the problem was too simple, the big guys could not see it), I found a way of my own.

===================================

First set up a simple text box

<EditText
        android:id="@+id/edt_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="350dp"
        android:height="200dp"
        android:hint="请输入少于文字"
        />

Then create three files in the drawable folder. The three files are:
edittext_style_normal (the style when the focus is not in the edit box)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <stroke
        android:width="1dp"
        android:color="#BDC7D8"/>
</shape>

edittext_style_focused (focus is on the text box)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <stroke
        android:width="1dp"
        android:color="#000000"/>
</shape>

Selector: edittext_sytle

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:drawable="@drawable/edittext_style_normal"></item>
    <item android:state_window_focused="true" android:drawable="@drawable/edittext_style_focused"></item>
</selector>

Then add the selector to the edittext style

<EditText
        android:id="@+id/edt_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="350dp"
        android:height="200dp"
        android:hint="请输入少于文字"
        android:background="@drawable/edittext_sytle"
        />

That's it!

Guess you like

Origin blog.csdn.net/weixin_44048668/article/details/111992248