You should use drawable selector to achieve your UI.
First create a background_edit_text_default.xml
which is the background of EditText when it is not selected by users.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#333D46" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
Then create a background_edit_text_selected.xml
which is the background of EditText when it is selected by users.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#EDB90E" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
Next create a background_edit_text.xml
which will be used as background of the EditText.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background_edit_text_default" android:state_focused="false" />
<item android:drawable="@drawable/background_edit_text_selected" android:state_focused="true" />
</selector>
Finally set background_edit_text.xml
as background of your EditText in your layout file, such as activity_main
.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/conteiner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_edit_text" />
<TextView
android:layout_width="match_parent"
android:layout_height="10dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_edit_text" />
</LinearLayout>
You're done and no need to add anything in code.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加