【Android自定义View实战】之自定义超简单SearchView搜索框

【Android自定义View实战】之自定义超简单SearchView搜索框


这篇文章是对之前文章的翻新,至于为什么我要重新修改这篇文章?原因如下

1.有人举报我抄袭,原文链接:http://www.it165.net/pro/html/201407/17779.html。呵呵...................................................................请大家仔细看看,那个图片水印。到底是谁抄袭谁呢?
2.之前的那篇文章写得非常随意,今天先到来封装一个自定义View,使用起来更方便。

在Android开发中我们经常会用到搜索框,而系统提供的又不尽完美。所以自定义一个比较简单的SearchView。代码非常简单,高手请略过。


效果图



实现代码

1.布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="12dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/search_bg"
        android:orientation="horizontal">

        <Button
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="right|center_vertical"
            android:layout_margin="10dp"
            android:background="@mipmap/search" />
        <!-- 输入的搜索信息 -->
        <EditText
            android:id="@+id/et_search"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_marginLeft="10dp"
            android:layout_weight="4"
            android:background="@null"
            android:gravity="center_vertical"
            android:hint="输入内容进行搜索"
            android:imeOptions="actionSearch"
            android:singleLine="true"
            android:textColor="#0e0e0e"
            android:textColorHint="#b0c6ce"
            android:textSize="17sp" />


        <Button
            android:id="@+id/bt_clear"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_gravity="right|center_vertical"
            android:layout_margin="10dp"
            android:background="@mipmap/delete" />
    </LinearLayout>


</LinearLayout>


2.java代码
package cn.bluemobi.dylan.searchview;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

/**
 * Android自定义SearchView
 * Created by yuandl 
 */

public class SearchView extends LinearLayout implements TextWatcher, View.OnClickListener {
    /**
     * 输入框
     */
    private EditText et_search;
    /**
     * 输入框后面的那个清除按钮
     */
    private Button bt_clear;


    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**加载布局文件*/
        LayoutInflater.from(context).inflate(R.layout.pub_searchview, this, true);
        /***找出控件*/
        et_search = (EditText) findViewById(R.id.et_search);
        bt_clear = (Button) findViewById(R.id.bt_clear);
        bt_clear.setVisibility(GONE);
        et_search.addTextChangedListener(this);
        bt_clear.setOnClickListener(this);
    }


    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    /****
     * 对用户输入文字的监听
     *
     * @param editable
     */
    @Override
    public void afterTextChanged(Editable editable) {
        /**获取输入文字**/
        String input = et_search.getText().toString().trim();
        if (input.isEmpty()) {
            bt_clear.setVisibility(GONE);
        } else {
            bt_clear.setVisibility(VISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        et_search.setText("");
    }
}


3.具体功能的实现
以上只是对界面的写法,下面是对 搜索记录功能实现: 【玩转SQLite系列】(六)SQLite数据库应用案例实现历史搜索记录


2.java代码
  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值