Android-AutoCompleteTextView实现历史搜索记录显示
今天尝试做了个Demo,效果如下:
当输入框获取焦点时,下拉显示历史搜索记录,AutoCompleteTextView自动补全。
需要用到的技术:1.SharedPreferences用来存储用户搜索的记,保存至本地
2.JsonArray存取SharedPreferences里存放的搜索记录
3.AutoCompleteText实现自动补全
布局代码如下:
<?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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:completionThreshold="1"
android:hint="关键词" />
<Button
android:id="@+id/query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="搜索" />
</LinearLayout>
<TextView
android:id="@+id/showText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="nothing to show!" />
</LinearLayout>
一定要写上android:completionThreshold="1",不然调用showDropDown()会报错,这属性意思是输入一个字符后显示匹配信息
Activity代码如下:
package com.example.zkl.myapplication;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
public class indexActivity extends Activity implements View.OnClickListener, View.OnFocusChangeListener {
private AutoCompleteTextView autoText;
ArrayAdapter<String> adapter;
private TextView textView;
private Button button;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_query);
autoText = findViewById(R.id.autoCompleteTextView);
autoText.setOnFocusChangeListener(this);
textView = findViewById(R.id.showText);
button = findViewById(R.id.query);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
addAutoComText();
this.textView.setText(autoText.getText());
}
/*
* 用json实现本地保存搜索记录
*/
private void addAutoComText() {
SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
JSONArray jsonArray = new JSONArray();
SharedPreferences.Editor editor;
if ("".equals(sharedPreferences.getString("queryHistory", ""))) {
jsonArray.put(autoText.getText());
} else {
try {
jsonArray=new JSONArray(sharedPreferences.getString("queryHistory", ""));
} catch (JSONException e) {
e.printStackTrace();
}
//最多保存五个,当达到5个时候删除最早的搜索记录
if (jsonArray.length() > 4) {
jsonArray.remove(0);
jsonArray.put(autoText.getText());
} else if (jsonArray.length() > 0) {
jsonArray.put(autoText.getText());
}
}
editor = sharedPreferences.edit();
editor.putString("queryHistory", jsonArray.toString());
editor.commit();
}
/*
* 读取jsonArray中的数据构建适配器,AutoCompleteText加载适配器
*/
private void updateDropDown() {
SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(sharedPreferences.getString("queryHistory", ""));
String str[] = new String[5];
for (int i = 0; i < jsonArray.length(); i++) {
str[i] = jsonArray.getString(i);
}
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, str);
autoText.setAdapter(adapter);
autoText.setCompletionHint("历史搜索");
} catch (JSONException e) {
e.printStackTrace();
}
}
/*
* 获取焦点时候显示历史搜索内容
*/
@Override
public void onFocusChange(View view, boolean b) {
if (view.getId() == R.id.autoCompleteTextView) {
this.updateDropDown();
autoText.showDropDown();
}
}
}
新手菜鸟一枚,欢迎讨论。