Android-AutoCompleteTextView实现历史搜索记录显示
来源:互联网 发布:java api pdf 编辑:程序博客网 时间:2024/05/21 14:50
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(); } }}
新手菜鸟一枚,欢迎讨论。
阅读全文
0 0
- Android-AutoCompleteTextView实现历史搜索记录显示
- 简单实现Android搜索功能 显示清除历史搜索记录
- 实现Android搜索功能 显示清除历史搜索记录
- 简单实现Android搜索功能 显示清除历史搜索记录
- AutoCompleteTextView搜索框显示记录的实现
- Android浏览器地址栏中历史访问记录的自动提示实现(AutoCompleteTextView)
- 使用AutoCompleteTextView和SharePreference实现搜索历史自动提示功能
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android流式布局实现历史搜索记录
- android 利用数据库实现历史搜索记录功能
- Android用AutoCompleteTextView实现搜索历史记录提示
- JS--历史搜索记录的实现
- Android常用:手把手教你实现搜索框(含历史搜索记录)
- Android常用:手把手教你实现搜索框(含历史搜索记录)
- 一个不错的HttpHelper
- pandas,横向Concat时遇到 Reindexing only valid with uniquely valued Index objects问题
- python 获取字符串中出现次数最多的字母
- Java
- 高并发静态页面化解决方案
- Android-AutoCompleteTextView实现历史搜索记录显示
- 用dep代替 go get 来获取私有库
- python之获取本地文件名称
- 2.3 寻找发帖“水王”
- jquery中使用eval()函数
- 分析redis的RDB和AOF两种持久化机制的工作原理
- VB 学习整理3 输入和输出
- Android Framework--PackageManagerService
- [协同过滤]:交替最小二乘法