package org.yaoming.read_his;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
public class Read_historyActivity extends Activity implements
OnClickListener {
private AutoCompleteTextView autoTv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
autoTv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
initAutoComplete("history",autoTv);
Button search = (Button) findViewById(R.id.button1);
search.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// 这里可以设定:当搜索成功时,才执行保存操作
saveHistory("history",autoTv);
}
/**
* 初始化AutoCompleteTextView,最多显示5项提示,使
* AutoCompleteTextView在一开始获得焦点时自动提示
* @param field 保存在sharedPreference中的字段名
* @param auto 要操作的AutoCompleteTextView
*/
private void initAutoComplete(String field,AutoCompleteTextView auto) {
SharedPreferences sp = getSharedPreferences("network_url", 0);
String longhistory = sp.getString("history", "nothing");
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.setAdapter(adapter);
auto.setDropDownHeight(350);
auto.setThreshold(1);
auto.setCompletionHint("最近的5条记录");
auto.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
AutoCompleteTextView view = (AutoCompleteTextView) v;
if (hasFocus) {
view.showDropDown();
}
}
});
}
/**
* 把指定AutoCompleteTextView中内容保存到sharedPreference中指定的字符段
* @param field 保存在sharedPreference中的字段名
* @param auto 要操作的AutoCompleteTextView
*/
private void saveHistory(String field,AutoCompleteTextView auto) {
String text = auto.getText().toString();
SharedPreferences sp = getSharedPreferences("network_url", 0);
String longhistory = sp.getString(field, "nothing");
if (!longhistory.contains(text + ",")) {
StringBuilder sb = new StringBuilder(longhistory);
sb.insert(0, text + ",");
sp.edit().putString("history", sb.toString()).commit();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
使用autoCompleteTextView读取历史记录
共24个文件
class:8个
xml:3个
png:3个
5星 · 超过95%的资源 需积分: 10 633 下载量 95 浏览量
2012-02-03
11:18:36
上传
评论 9
收藏 44KB RAR 举报
温馨提示
具体可查看我的博客http://blog.csdn.net/iamkila?viewmode=contents
资源推荐
资源详情
资源评论
收起资源包目录
read_history.rar (24个子文件)
read_history
bin
classes.dex 5KB
resources.ap_ 11KB
org
yaoming
read_his
R$layout.class 400B
R$id.class 436B
R$drawable.class 406B
R$attr.class 346B
Read_historyActivity$1.class 1KB
R.class 545B
R$string.class 433B
Read_historyActivity.class 4KB
read_history.apk 15KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
values
strings.xml 183B
drawable-mdpi
icon.png 3KB
layout
main.xml 860B
assets
default.properties 362B
gen
org
yaoming
read_his
R.java 805B
proguard.cfg 1KB
.settings
org.eclipse.core.resources.prefs 88B
src
org
yaoming
read_his
Read_historyActivity.java 3KB
.project 848B
.classpath 280B
AndroidManifest.xml 694B
共 24 条
- 1
iamkila
- 粉丝: 51
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页