
【Android自己定义View实战】之自己定义超简单SearchView搜索框
这篇文章是对之前文章的翻新,至于为什么我要又一次改动这篇文章?原因例如以下
1.有人举报我抄袭,原文链接:http://www.it165.net/pro/html/201407/17779.html。
呵呵...................................................................请大家细致看看,那个图片水印。
究竟是谁抄袭谁呢?
2.之前的那篇文章写得很任意。今天先到来封装一个自己定义View。使用起来更方便。
在Android开发中我们常常会用到搜索框。而系统提供的又不尽完美。
所以自己定义一个比較简单的SearchView。
代码很easy,高手请略过。
效果图

raw=true" alt="" />
实现代码
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;
/**
* 输入框后面的那个清除button
*/
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代码