自定义android EditText样式及使用方式

来源:互联网 发布:品牌网络推广渠道 编辑:程序博客网 时间:2024/05/22 07:00

开发中需要对EditText样式进行特殊化定义,使用selector进行获取焦点和失去焦点的样式设计:

代码实现:

searchText.setBackgroundResource(R.drawable.search_edit_selector);


所有的样式和selector都放在drawable目录;

edit_selector.xml书写方式:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <item android:drawable="@drawable/search_edittext_style" android:state_window_focused="false"/>
    <item android:drawable="@drawable/search_edittext_style_focused" android:state_focused="true"/>


</selector>


search_edittext_style.xml样式书写,使用layer-list 实现:


<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


    <item>


        <!-- //框为矩形 -->
        <shape android:shape="rectangle" >

            <!-- //用白色来填充里面 -->
            <solid android:color="#BBDCE0" />
            <!-- //边框是1dp的线 -->
            <stroke
                android:width="1dp"
                android:color="#FFFFFF" />
        </shape>
    </item>


</layer-list>


search_edittext_style_focused.xml样式书写:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


    <item>


        <!-- //框为矩形 -->
        <shape android:shape="rectangle" >

            <!-- //用白色来填充里面 -->
            <solid android:color="#FFFFFF" />
            <!-- //边框是1dp的线 -->
            <stroke
                android:width="1dp"
                android:color="#FFFFFF" />
        </shape>
    </item>


</layer-list>



0 0