Android 用EditText加一个ImageButton自定义一个带clear的搜索框

    最近在做一个项目,里面的通讯录功能有个需求:根据汉字姓名、拼音姓名、手机号码对通讯录中的联系人进行搜索,刚开始想到了使用SearchView,但是用了以后感觉该控件不够灵活,达不到UI设计的要求。所以就想到了自定义控件实现SearchView的功能,本文就带领大家一步步用EditText和ImageButton实现一个自定义的SearchView。

    1.首先是自定义搜索框的布局文件 search_bar_contact.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#eeeeee" >

    <ImageButton
        android:id="@+id/search_back_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@null"
        android:contentDescription="@string/app_name"
        android:src="@drawable/back" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@id/search_back_btn"
        android:background="@drawable/seabar_input" >

        <EditText
            android:id="@+id/query"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="4dp"
            android:background="@null"
            android:drawableLeft="@drawable/search_bar_icon_normal"
            android:hint="搜索"
            android:paddingBottom="3dp"
            android:paddingLeft="5dp"
            android:paddingTop="3dp"
            android:singleLine="true"
            android:textSize="16sp" />

        <ImageButton
            android:id="@+id/search_clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@android:color/transparent"
            android:contentDescription="@string/app_name"
            android:src="@drawable/clear"
            android:visibility="gone" />
    </RelativeLayout>

</RelativeLayout>

   2.其次是实现搜索及显示数据的Activity关联的布局文件 search_contact.xml,该布局文件通过include标签引用了上面的布局文件search_bar_contact.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" >

    <include
        android:id="@+id/search_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/search_bar_contact" />

    <TextView
        android:id="@+id/rule_line_tv"
        an
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值