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

本文介绍了如何在Android中为EditText使用Selector作为背景,详细阐述了默认状态和选中状态的shape设置,并通过示例代码展示其应用。同时,提到了如何复用style来优化代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        <!--
            这里不能使用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>

默认状态和选中状态显示
在这里插入图片描述![在这里插入图片描述](https://img-blog.csdnimg.cn/20200922162944156.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMDgyMzE0,size_16,color_FFFFFF,t_70#pic_center

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值