前言

  • 像下图的搜索功能在Android开发中非常常见

  • 今天我将手把手教大家如何实现具备历史搜索记录的搜索框

目录


1. 使用场景

在敲下代码前,理解用户的功能使用场景是非常重要的,这样有助于我们更好地去进行功能的实现,使用场景如下:

  • 用户需要进行某类事物的搜索(通过文字输入进行精确搜索)
  • 在搜索框输入时,通过显示搜索历史从而降低用户二次搜索的成本

简单来说,就是输入过字段会保存,当用户再次搜索该字段时,能快速帮助用户输入


2. 功能业务流程


3. 明确功能点

  • 功能1:关键字搜索
  • 功能2:实时显示历史搜索记录
  • 功能3:历史搜索记录保存
  • 功能4:将软键盘上的”回车按钮“改为”搜索按钮“

4. 涉及到的知识点

4.1 SQLite数据库的增删改查操作

  • 在数据库建立一个叫records的表用于存储搜索历史记录

    里面只有一列name来存储历史记录

        db.execSQL("create table records(id integer primary key autoincrement,name varchar(200))");
  • 1
  • 1
  • 通过插入和删除操作数据库里面的数据
  • 通过查询操作来显示数据库里面的数据到ListView上 

    已搜索的关键字再次搜索不重复添加到数据库

注:对于SQLlite数据库的操作详情请看我写的文章: Android:SQLlite数据库操作最详细解析

4.2 ListView和ScrollView的嵌套冲突

  • 问题:ListView和ScrollView一起使用会有冲突,导致ListView显示不全
  • 解决方案:本人采用继承ListView并重写它的onMeasure()方法来解决冲突。

    1. 至于两者的滑动冲突则暂时不需要处理(默认拉动ScrollView),这样用户就会感觉里面的搜索历史项和清空搜索记录项是在ListView里面一样,符合设计。
    2. 更多解决方法请看:ListView和ScrollView的嵌套冲突解决方案
  • 具体代码如下:

//解决ListView和ScrollView的冲突
public class Search_Listview extends ListView {public Search_Listview(Context context) {super(context);}//通过复写其onMeasure方法、达到对ScrollView适配的效果@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expandSpec);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4.3 监听软键盘回车按钮设置为搜索按钮

给搜索框EditText添加一个OnKeyListener监听器,重写里面的回车键,实现按下回车键搜索。

et_search.setOnKeyListener(new View.OnKeyListener() {// 输入完后按键盘上的搜索键// 修改回车键功能public boolean onKey(View v, int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {写入搜索操作和数据库插入搜索记录操作
}});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4.4 使用TextWatcher实时筛选

给搜索框EditText添加一个TextWatcher来检测EditText里面的每个字符变化,根据变化之后的内容查询数据库得到结果。

 et_search.addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}//输入有变化调用该方法@Overridepublic void afterTextChanged(Editable s) {       //设置查询数据库并显示在列表的操作    }});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

5. 实例Demo

接下来,我将给出详细代码手把手教你实现具备历史搜索记录的搜索框

先下载Demo再进行阅读效果会更好:Carson的Search_Layout_Demo地址

5.1 目录结构

5.2 具体实现如下:

MainActivity.Java

  • 作用:显示搜索框
  • 具体代码如下:
package scut.carson_ho.search_layout;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

RccordSQLiteOpenHelper.java

  • 作用:继承自SQLiteOpenHelper数据库类的子类,用于创建、管理数据库和版本控制
  • 具体代码如下:

对于SQLlite数据库的操作详情请看我写的文章:Android:SQLlite数据库操作最详细解析

package scut.carson_ho.search_layout;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;/*** Created by Carson_Ho on 16/11/15.*/
//SQLiteOpenHelper子类用于打开数据库并进行对用户搜索历史记录进行增删减除的操作
public class RecordSQLiteOpenHelper extends SQLiteOpenHelper {private static String name = "temp.db";private static Integer version = 1;public RecordSQLiteOpenHelper(Context context) {super(context, name, null, version);}@Overridepublic void onCreate(SQLiteDatabase db) {//打开数据库,建立了一个叫records的表,里面只有一列name来存储历史记录:db.execSQL("create table records(id integer primary key autoincrement,name varchar(200))");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

Search_ListView.java

  • 作用:用于解决ListView和ScrollView的嵌套冲突
  • 具体代码如下:
package scut.carson_ho.search_layout;import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;/*** Created by Carson_Ho on 16/11/15.*/
//解决ListView和ScrollView的冲突
public class Search_Listview extends ListView {public Search_Listview(Context context) {super(context);}public Search_Listview(Context context, AttributeSet attrs) {super(context, attrs);}public Search_Listview(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}//通过复写其onMeasure方法、达到对ScrollView适配的效果@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expandSpec);}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

Search_View.java

  • 作用:用于封装搜索框功能的所有操作(涵盖历史搜索记录的插入、删除、查询和显示)
  • 具体代码如下:
package scut.carson_ho.search_layout;import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;/*** Created by Carson_Ho on 16/11/15.*/
public class Search_View extends LinearLayout {private Context context;/*UI组件*/private TextView tv_clear;private EditText et_search;private TextView tv_tip;private ImageView iv_search;/*列表及其适配器*/private Search_Listview listView;private BaseAdapter adapter;/*数据库变量*/private RecordSQLiteOpenHelper helper ;private SQLiteDatabase db;/*三个构造函数*///在构造函数里直接对搜索框进行初始化 - init()public Search_View(Context context) {super(context);this.context = context;init();}public Search_View(Context context, AttributeSet attrs) {super(context, attrs);this.context = context;init();}public Search_View(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);this.context = context;init();}/*初始化搜索框*/private void init(){//初始化UI组件initView();//实例化数据库SQLiteOpenHelper子类对象helper = new RecordSQLiteOpenHelper(context);// 第一次进入时查询所有的历史记录queryData("");//"清空搜索历史"按钮tv_clear.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//清空数据库deleteData();queryData("");}});//搜索框的文本变化实时监听et_search.addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}//输入后调用该方法@Overridepublic void afterTextChanged(Editable s) {if (s.toString().trim().length() == 0) {//若搜索框为空,则模糊搜索空字符,即显示所有的搜索历史tv_tip.setText("搜索历史");} else {tv_tip.setText("搜索结果");}//每次输入后都查询数据库并显示//根据输入的值去模糊查询数据库中有没有数据String tempName = et_search.getText().toString();queryData(tempName);}});// 搜索框的键盘搜索键// 点击回调et_search.setOnKeyListener(new View.OnKeyListener() {// 输入完后按键盘上的搜索键// 修改回车键功能public boolean onKey(View v, int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {// 隐藏键盘,这里getCurrentFocus()需要传入Activity对象,如果实际不需要的话就不用隐藏键盘了,免得传入Activity对象,这里就先不实现了
//                    ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
//                            getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);// 按完搜索键后将当前查询的关键字保存起来,如果该关键字已经存在就不执行保存boolean hasData = hasData(et_search.getText().toString().trim());if (!hasData) {insertData(et_search.getText().toString().trim());queryData("");}//根据输入的内容模糊查询商品,并跳转到另一个界面,这个需要根据需求实现Toast.makeText(context, "点击搜索", Toast.LENGTH_SHORT).show();}return false;}});//列表监听//即当用户点击搜索历史里的字段后,会直接将结果当作搜索字段进行搜索listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {//获取到用户点击列表里的文字,并自动填充到搜索框内TextView textView = (TextView) view.findViewById(android.R.id.text1);String name = textView.getText().toString();et_search.setText(name);Toast.makeText(context, name, Toast.LENGTH_SHORT).show();}});//点击搜索按钮后的事件iv_search.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {boolean hasData = hasData(et_search.getText().toString().trim());if (!hasData) {insertData(et_search.getText().toString().trim());//搜索后显示数据库里所有搜索历史是为了测试queryData("");}//根据输入的内容模糊查询商品,并跳转到另一个界面,这个根据需求实现Toast.makeText(context, "clicked!", Toast.LENGTH_SHORT).show();}});}/*** 封装的函数*//*初始化组件*/private void initView(){LayoutInflater.from(context).inflate(R.layout.search_layout,this);et_search = (EditText) findViewById(R.id.et_search);tv_clear = (TextView) findViewById(R.id.tv_clear);tv_tip = (TextView) findViewById(R.id.tv_tip);listView = (Search_Listview) findViewById(R.id.listView);iv_search = (ImageView) findViewById(R.id.iv_search);}/*插入数据*/private void insertData(String tempName) {db = helper.getWritableDatabase();db.execSQL("insert into records(name) values('" + tempName + "')");db.close();}/*模糊查询数据 并显示在ListView列表上*/private void queryData(String tempName) {//模糊搜索Cursor cursor = helper.getReadableDatabase().rawQuery("select id as _id,name from records where name like '%" + tempName + "%' order by id desc ", null);// 创建adapter适配器对象,装入模糊搜索的结果adapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_1, cursor, new String[] { "name" },new int[] { android.R.id.text1 }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);// 设置适配器listView.setAdapter(adapter);adapter.notifyDataSetChanged();}/*检查数据库中是否已经有该条记录*/private boolean hasData(String tempName) {//从Record这个表里找到name=tempName的idCursor cursor = helper.getReadableDatabase().rawQuery("select id as _id,name from records where name =?", new String[]{tempName});//判断是否有下一个return cursor.moveToNext();}/*清空数据*/private void deleteData() {db = helper.getWritableDatabase();db.execSQL("delete from records");db.close();}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242

search_layout.xml

  • 作用:搜索框的布局
  • 具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:focusableInTouchMode="true"android:orientation="vertical"><LinearLayout
        android:layout_width="fill_parent"android:layout_height="50dp"android:background="#E54141"android:orientation="horizontal"android:paddingRight="16dp"><ImageView
            android:layout_width="45dp"android:layout_height="45dp"android:layout_gravity="center_vertical"android:padding="10dp"android:src="@drawable/back" /><EditText
            android:id="@+id/et_search"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="264"android:background="@null"android:drawablePadding="8dp"android:gravity="start|center_vertical"android:hint="输入查询的关键字"android:imeOptions="actionSearch"android:singleLine="true"android:textColor="@android:color/white"android:textSize="16sp" /><ImageView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/search"android:layout_gravity="center_vertical"android:id="@+id/iv_search"/></LinearLayout><ScrollView
        android:layout_width="wrap_content"android:layout_height="wrap_content"><LinearLayout
            android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayout
                android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:paddingLeft="20dp"><TextView
                    android:id="@+id/tv_tip"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="left|center_vertical"android:text="搜索历史" /><View
                    android:layout_width="match_parent"android:layout_height="1dp"android:background="#EEEEEE"/><scut.carson_ho.search_layout.Search_Listview
                    android:id="@+id/listView"android:layout_width="match_parent"android:layout_height="wrap_content"></scut.carson_ho.search_layout.Search_Listview></LinearLayout><View
                android:layout_width="match_parent"android:layout_height="1dp"android:background="#EEEEEE"/><TextView
                android:id="@+id/tv_clear"android:layout_width="match_parent"android:layout_height="40dp"android:background="#F6F6F6"android:gravity="center"android:text="清除搜索历史" /><View
                android:layout_width="match_parent"android:layout_height="1dp"android:layout_marginBottom="20dp"android:background="#EEEEEE"/></LinearLayout></ScrollView>
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105

activity_main.xml

  • 作用:显示搜索框
  • 具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="scut.carson_ho.search_layout.MainActivity"><scut.carson_ho.search_layout.Search_View
        android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/search_layout"/>
</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

5.3 测试结果

5.4 Demo地址

Carson的Github地址:Search_Layout_Demo

实现搜索框(含历史搜索记录)相关推荐

  1. position:搜索框显示历史浏览记录

    absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位. 元素的位置通过 "left", "top", "righ ...

  2. 有历史搜索记录的搜索框(百度搜索案例)

    带有历史搜索词条的搜索框 记录一下带有历史搜索词条的搜索框,具体效果类似百度搜索: 点击搜索框 展示历史搜索词条列表: 点击搜索框以外的区域 隐藏历史搜索词条列表: 点击删除除去对应词条. 效果图: ...

  3. Android开源实战:简单好用、含历史搜索记录的智能搜索框

    前言 Android开发中,类似下图的搜索功能非常常见 今天,我将带来一款 封装了 历史搜索记录功能 & 样式 的Android 自定义搜索框 开源库,希望你们会喜欢. 已在Github开源: ...

  4. Android开源实战:SearchView搜索框(含历史搜索记录)

    前言 Android开发中,类似下图的搜索功能非常常见 搜索功能 今天,我将手把手教大家实现一款 封装了 历史搜索记录功能 & 样式 的Android 自定义搜索框 开源库,希望你们会喜欢. ...

  5. Android常用:手把手教你实现搜索框(含历史搜索记录)

    http://blog.csdn.net/carson_ho/article/details/53366570 前言 像下图的搜索功能在Android开发中非常常见 今天我将手把手教大家如何实现具备历 ...

  6. Android 系统搜索框(有浏览记录)

    实现Android 系统搜索框(有浏览记录),先看下效果: 一.配置搜索描述文件 要在res中的xml文件加创建sreachable.xml,内容如下: <?xml version=" ...

  7. 百度搜索结果的URL参数 搜索框提示词搜索方式(rsv_sug2)

    通过百度搜索框提示,看到可能想找的关键词之后,百度提供了2种方式供选择,并且用参数 rsv_sug2 记录了用户的选择. 百度搜索结果网址参数 搜索框提示词搜索方式(rsv_sug2) 名称 rsv_ ...

  8. 百度搜索引擎结果网址参数 搜索框提示词搜索方式(rsv_sug2)

    通过百度搜索框提示,看到可能想找的关键词之后,百度提供了2种方式供选择,并且用参数 rsv_sug2 记录了用户的选择. [b]百度搜索结果网址参数 搜索框提示词搜索方式(rsv_sug2)[/b] ...

  9. 百度搜索结果页面的参数 搜索框提示词搜索方式(rsv_sug2)

    通过百度搜索框提示,看到可能想找的关键词之后,百度提供了2种方式供选择,并且用参数 rsv_sug2 记录了用户的选择. 百度搜索结果网址参数 搜索框提示词搜索方式(rsv_sug2) 名称 rsv_ ...

  10. 如何清除搜索框内的搜索历史记录?

    如何清除搜索框内的搜索历史记录? 答:这是网页浏览器的一项功能.进入IE浏览器的相关菜单选项设置: ·如果您使用IE4.0浏览器,则由"查看→internet选项→内容→自动完成→清除表单→ ...

最新文章

  1. vue双向数据绑定原理分析--Mr.Ember
  2. MySQL数据类型合集
  3. c++ vector方法
  4. 图论-最长路--关于最长路的探讨2
  5. 力扣172.阶乘后的零
  6. UBUNTU设置SSH通过密钥登陆
  7. Dynamic Web TWAIN:网页扫描过程中如何自动检测和删除空白页?
  8. 2021年软件质量事故盘点
  9. 微信Windows版如何从旧电脑迁移聊天记录到新电脑
  10. 开服当GM的基本准则
  11. 微信网页版协议今天关闭,新的微信协议 HOOK技术
  12. 瑞典皇家理工学院计算机科学世界排名,2017瑞典皇家理工学院世界排名
  13. gitlab提交代码提示The remote end hung up unexpectedly
  14. Word中如何快速删除页眉下的横线?教你一招,轻松解决!
  15. python用角度计算余弦值_Python 使用sklearn计算余弦相似度
  16. 树莓派上安装 ROS Kinetic
  17. HTML-浮动与清除浮动
  18. 『.NET Tools』C#/.NET 计算文本相似度
  19. 干货分享|编辑视频有哪些选择,常见视频剪辑软件分享
  20. 地震资料处理Omega2014相关信息汇总

热门文章

  1. 题8.9:写一函数,将一个3x3的整型矩阵转置。
  2. 蚂蚁金融科技全面开放战略背后的技术布局
  3. 图书馆管理系统Python+MySQL+tkinter图形化界面+管理员登录+学生登录(注释详细)
  4. jQuery 插件库
  5. Android 测试工具集01
  6. 255%可用已接通电源_直流稳压电源五款电路图详解
  7. 如何快速提高自己的领导力?
  8. 第一篇自用博客:git的操作(防忘记用的hh)封面是我最爱的歌手ikura强推她的每一首歌,简直就是行走的唱片啊有木有!
  9. 以“掌上东航”为例,论混合开发在企业级项目中的实践
  10. while(i--)