您的位置:首页 > 移动开发 > Android开发

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

2014-07-18 16:15 615 查看

【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数据库应用案例实现历史搜索记录

GitHub
2.java代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息