//第一步 drawable里写个选中时的布局文件和未选中时的布局文件。
//未选中时的布局文件,浅色的边框,bg_edittext_normal.xml。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <corners android:radius="10dp"/> <stroke android:width="5dp" android:color="#BDC7D8"/> </shape>
//选中时的布局文件,深色的边框,bg_edittext_focused.xml。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF"/> <corners android:radius="10dp"/> <stroke android:width="5dp" android:color="#728ea3"/> </shape>
//第二步 再写个selector选择器。bg_edittext.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:drawable="@drawable/bg_edittext_normal"/> <item android:state_focused="true" android:drawable="@drawable/bg_edittext_focused"/> </selector>
//第三步 在EditText布局文件里引用,通过background。
//我的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.hasee.a921ceshi.Main2Activity"> <EditText android:padding="20dp" android:layout_margin="20dp" android:background="@drawable/bg_edittext" android:hint="输入输入" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:padding="20dp" android:layout_margin="20dp" android:background="@drawable/bg_edittext" android:hint="输入2输入2" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
//--------------------------------------------------------------------完----------------------------------------------------------------------------------