package org.yaoming.read_his;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutoTextView extends Activity {
private AutoCompleteTextView autoTv;
private static final String HISTORY = "search_history";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
autoTv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
initAutoComplete("history", autoTv);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
saveHistoryToSharedPreference("history", autoTv);// 这里可以设定:当搜索成功时,才执行保存操作
}
});
}
/**
* 初始化AutoCompleteTextView,最多显示5项提示,使 AutoCompleteTextView在一开始获得焦点时自动提示
*
* @param field
* 保存在sharedPreference中的字段名
* @param auto
* 要操作的AutoCompleteTextView
*/
private void initAutoComplete(String field, AutoCompleteTextView auto) {
SharedPreferences sp = getSharedPreferences(HISTORY, 0);
String longhistory = sp.getString("history", "");
String[] hisArrays = longhistory.split(",");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, hisArrays);
// 只保留最近的50条的记录
if (hisArrays.length > 50) {
String[] newArrays = new String[50];
System.arraycopy(hisArrays, 0, newArrays, 0, 50);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, newArrays);
}
auto.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
autoTv.showDropDown();
}
});
auto.setAdapter(adapter);
auto.setDropDownHeight(200);// 下拉的高度
auto.setThreshold(1);
auto.setCompletionHint("搜索记录");// 提示字段
}
/**
* 把指定AutoCompleteTextView中内容保存到sharedPreference中指定的字符段
*
* @param field
* 保存在sharedPreference中的字段名
* @param auto
* 要操作的AutoCompleteTextView
*/
private void saveHistoryToSharedPreference(String field,
AutoCompleteTextView auto) {
String text = auto.getText().toString();
SharedPreferences sp = getSharedPreferences(HISTORY, 0);
String longhistory = sp.getString(field, "");
if (!longhistory.contains(text + ",")) {// 不存在重复的
StringBuilder sb = new StringBuilder(longhistory);
sb.insert(0, text + ",");
sp.edit().putString("history", sb.toString()).commit();
}
initAutoComplete("history", autoTv);
}
}
- 大小: 15.5 KB
分享到:
相关推荐
AutoCompleteTextView实现历史记录功能,可正常运行。
具体可查看我的博客http://blog.csdn.net/iamkila?viewmode=contents
AutoCompleteTextView历史记录
AutoCompleteTextViewDemo,实现显示历史记录功能。
autoCompleteTextView自定义BaseAdapter,实现下拉单的自定义样式,用sqlite数据库存放历史记录,进行写入时提示。点击某个下拉单写入autoCompleeTextView中。
android 实现历史记录提示 AutoCompleteTextView 代码量小只要有一定的基础都能看懂。
网上有不少教程,那个提示框字符集都是事先写好的,例如用一个String[] 数组去包含了这些数据,但是,我们也可以吧用户输入的作为历史记录保存 下面先上我写的代码: import android.app.Activity; import ...
该demo使用了自定义的流式布局+AutoCompleteTextView+SharedPreferences实现了各大APP常用到的搜索历史记录的功能,代码简单实用。
SharedPreferences 保存读取数据, 使用AutoCompleteTextView 和SharedPreferences 实现历史记录提示。调用文件管理器自定义路径读取文件