美文网首页
EditText的背景选择器 Selector(包含复用styl

EditText的背景选择器 Selector(包含复用styl

作者: 追风z | 来源:发表于2020-09-23 22:31 被阅读0次
        <!--
            这里不能使用maxLength限制最长字符数
            因为我们这里用户名还有邮箱

            这里不能使用maxLength限制最长字符数
            因为我们这里用户名还有邮箱所以用textEmailAddress
            textEmailAddress:用这个可以输入邮箱和手机号,
            后面可以在代码中判断是手机号还是邮箱
       -->
        <EditText
            android:id="@+id/et_username"
            style="@style/EditText"
            android:hint="@string/enter_username"
            android:inputType="textEmailAddress" />
    <style name="EditText">
        <!--    宽高    -->
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <!-- 图片和文字距离   图片左边图片 -->
        <item name="android:drawablePadding">@dimen/padding_middle</item>
        <item name="android:drawableLeft">@drawable/ic_phone</item>
        <!-- 内边距 顶部和底部的距离       -->
        <item name="android:paddingTop">@dimen/padding_middle</item>
        <item name="android:paddingBottom">@dimen/padding_middle</item>

        <item name="android:layout_marginTop">@dimen/padding_middle</item>
        <!--最大显示一行-->
        <item name="android:lines">1</item>
        <!--编辑框EditText背景 默认状态和选中状态-->
        <item name="android:background">@drawable/selector_edit_text</item>
    </style>

背景选择器: selector_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--获取到焦点状态-->
    <item android:drawable="@drawable/shape_edit_text_selected" android:state_focused="true" />
    <!--默认状态-->
    <item android:drawable="@drawable/shape_edit_text" />
</selector>

//默认状态 shape

<?xml version="1.0" encoding="utf-8"?>
<!--layer-list:也是一种drawable
        类似ps中的图层-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--一个Item就是一层
        top:指定这一边到控件这一面的间距
        0dp:表示和原来一样
        正数:就是边框变小
        负数:边框显示到控件外面(从而达到隐藏效果)

        这里:就是顶部 左边 右边的边框往外边偏移
    -->
    <item
        android:top="-5dp"
        android:left="-5dp"
        android:right="-5dp"
        >

        <!--在一个Item中定义一个shape
            可以定义多个-->
        <shape>
            <!--填充颜色为透明-->
            <solid android:color="@color/transparent"/>
            <stroke android:width="@dimen/divider_small"
                    android:color="@color/light_grey"/>
        </shape>

    </item>

</layer-list>

//选中状态 shape

<?xml version="1.0" encoding="utf-8"?>
<!--layer-list:也是一种drawable
    类似ps中的图层
    当然也可以不用图层,可以直接用shape
    但是我们这类只显示4边中的一边,所以直接用图层

    这个是选中的形状
    -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--一个Item就是一层
    top:指定这一边到控件这一面的间距
    0dp:表示和原来一样
    正数:就是边框变小
    负数:边框显示到控件外面(从而达到隐藏效果)
    -->
    <item android:top="-5dp"
        android:right="-5dp"
        android:left="-5dp">
        <!--在一个Item中定义一个shape
        可以定义多个-->
        <shape>
            <!--填充颜色为透明-->
            <solid android:color="@color/transparent"/>

            <!--边框为灰色-->
            <stroke android:width="@dimen/divider_small"
                android:color="@color/colorPrimary"/>
        </shape>
    </item>
</layer-list>

默认状态和选中状态显示


image.png image.png

相关文章

网友评论

      本文标题:EditText的背景选择器 Selector(包含复用styl

      本文链接:https://www.haomeiwen.com/subject/ngwwyktx.html