1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6.  
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="horizontal" >
  11.  
  12. <AutoCompleteTextView
  13. android:id="@+id/auto"
  14. android:layout_width="0dp"
  15. android:layout_height="wrap_content"
  16. android:layout_weight="1"
  17. android:completionHint="最近5条记录"
  18. android:completionThreshold="1"
  19. />
  20.  
  21. <Button
  22. android:id="@+id/search"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="搜索" />
  26. </LinearLayout>
  27.  
  28. <LinearLayout
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:orientation="vertical" >
  32.  
  33. <Button
  34. android:id="@+id/clean"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:text="清除历史记录"
  38. android:onClick="cleanHistory"
  39. />
  40. </LinearLayout>
  41.  
  42. </LinearLayout>
  1. public class TestActivity extends Activity {
  2. private AutoCompleteTextView auto;
  3. private Button searchbtn;
  4. private ArrayAdapter<String> arr_adapter;
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.test);
  10.  
  11. // 初始化
  12. auto = (AutoCompleteTextView) findViewById(R.id.auto);
  13. searchbtn = (Button) findViewById(R.id.search);
  14.  
  15. // 获取搜索记录文件内容
  16. SharedPreferences sp = getSharedPreferences("search_history", 0);
  17. String history = sp.getString("history", "暂时没有搜索记录");
  18.  
  19. // 用逗号分割内容返回数组
  20. String[] history_arr = history.split(",");
  21.  
  22. // 新建适配器,适配器数据为搜索历史文件内容
  23. arr_adapter = new ArrayAdapter<String>(this,
  24. android.R.layout.simple_dropdown_item_1line, history_arr);
  25.  
  26. // 保留前50条数据
  27. if (history_arr.length > 50) {
  28. String[] newArrays = new String[50];
  29. // 实现数组之间的复制
  30. System.arraycopy(history_arr, 0, newArrays, 0, 50);
  31. arr_adapter = new ArrayAdapter<String>(this,
  32. android.R.layout.simple_dropdown_item_1line, history_arr);
  33. }
  34.  
  35. // 设置适配器
  36. auto.setAdapter(arr_adapter);
  37.  
  38. // 设置监听事件,点击搜索写入搜索词
  39. searchbtn.setOnClickListener(new Button.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. // TODO Auto-generated method stub
  43. save();
  44. }
  45.  
  46. });
  47.  
  48. }
  49.  
  50. public void save() {
  51. // 获取搜索框信息
  52. String text = auto.getText().toString();
  53. SharedPreferences mysp = getSharedPreferences("search_history", 0);
  54. String old_text = mysp.getString("history", "暂时没有搜索记录");
  55.  
  56. // 利用StringBuilder.append新增内容,逗号便于读取内容时用逗号拆分开
  57. StringBuilder builder = new StringBuilder(old_text);
  58. builder.append(text + ",");
  59.  
  60. // 判断搜索内容是否已经存在于历史文件,已存在则不重复添加
  61. if (!old_text.contains(text + ",")) {
  62. SharedPreferences.Editor myeditor = mysp.edit();
  63. myeditor.putString("history", builder.toString());
  64. myeditor.commit();
  65. Toast.makeText(this, text + "添加成功", Toast.LENGTH_SHORT).show();
  66. } else {
  67. Toast.makeText(this, text + "已存在", Toast.LENGTH_SHORT).show();
  68. }
  69.  
  70. }
  71.  
  72. //清除搜索记录
  73. public void cleanHistory(View v){
  74. SharedPreferences sp =getSharedPreferences("search_history",0);
  75. SharedPreferences.Editor editor=sp.edit();
  76. editor.clear();
  77. editor.commit();
  78. Toast.makeText(this, "清除成功", Toast.LENGTH_SHORT).show();
  79. super.onDestroy();
  80. }
  81.  
  82. }

实例下载>>>>

相关文章:

储存方式之SharePreferences

AutoCompleteTextView 自动提示

Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示的更多相关文章

  1. Android——控件AutoCompleteTextView 自动提示

    Android:控件AutoCompleteTextView 自动提示 在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextV ...

  2. Android 控件 -------- AutoCompleteTextView 动态匹配内容,例如 百度搜索提示下拉列表功能

    AutoCompleteTextView 支持基本的自动完成功能,适用在各种搜索功能中,并且可以根据自己的需求设置他的默认显示数据.两个控件都可以很灵活的预置匹配的那些数据,并且可以设置输入多少值时开 ...

  3. Android控件——AutoCompleteTextView与MultiAutoCompleteTextView(实现自动匹配输入的内容)

    ------------------------------------AutoCompleteTextView----------------------

  4. Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度

    Android AutoCompleteTextView 控件实现类似被搜索提示,效果如下 1.首先贴出布局代码 activity_main.xml: <?xml version="1 ...

  5. 一步一步学android控件(之六) —— MultiAutoCompleteTextView

    今天学习的控件是MultiAutoCompleteTextView . 提到MultiAutoCompleteTextView 我们就自然而然地想到AutoCompleteTextView ,就想知道 ...

  6. 从Android系统出发,分析Android控件构架

    从Android系统出发,分析Android控件构架 Android中所有的控件追溯到根源,就是View 和ViewGroup,相信这个大家都知道,但是大家也许会不太清楚它们之间的具体关系是什么,在A ...

  7. android控件的属性

    android控件的属性 本节描述android空间的位置,内容等相关属性及属性的含义 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ( ...

  8. Robotium之Android控件定位实践和建议(Appium/UIAutomator姊妹篇)

    本人之前以前撰文描写叙述Appium和UIAutomator框架是怎样定位Android界面上的控件的. UIAutomator定位Android控件的方法实践和建议 Appium基于安卓的各种Fin ...

  9. Robotium之Android控件定位实践和建议

    本人之前曾经撰文描述Appium和UIAutomator框架是如何定位Android界面上的控件的. UIAutomator定位Android控件的方法实践和建议Appium基于安卓的各种FindEl ...

随机推荐

  1. 11g RAC R2 体系结构---用户及用户组

    10.2 RAC 到11.2 RAC 用户及用户组的变化: 在10.2 RAC 的部署中,只需要一个用户(oracle)和一个用户组(dba).Database.Clusterware都是用oracl ...

  2. 客户端访问WebService和PageMethod

    客户端访问WebService 客户端访问WebService和后台访问WebService没什么不同,注意的地方是要在ScriptManager中添加 <Services>        ...

  3. mac os使用homebrew来管理后台服务

    在linux下我们经常通过 service 或者 /etc/init.d/来管理我们的后台服务软件,并使用包管理器安装这些软件. 在mac下有homebrew这个好用的工具来安装软件,但是一直没有找到 ...

  4. objdump的使用方法和 symbol table的每列的含义

    一.objdump的用法 objdump命令的man手册 objdump     [-a] [-b bfname|     --target=bfdname] [-C] [--debugging]   ...

  5. django 更新model

    修改models.py 中对应的class 在admin.py 中 增加 admin.site.register(WafDevice) 进入dbshell python manage.py dbshe ...

  6. Ubuntu首次开启root用户

    最近一直在学习linux,选择ubuntu作为联系的操作系统.然后一直发现自己所创建的用户和root用户不是一个概念,执行好多命令的时候都提示没有权限.这样,最后终于发现原来是ubuntu是默认关闭r ...

  7. 扒一扒各大电商网站的m站都用的什么前端技术输入日志标题

    凡客首页使用Swiper和zepto,没有使用jquery , 静态首页+js交互,  资源加载使用 lazyLoad X-AspNet-Version: 4.0.30319 X-AspNetMvc- ...

  8. 屏蔽ios7中某个页面的默认手势滑回返回

    - (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:YES];self.navigationController.in ...

  9. 好项目烂架构的问题,四年coder的吐槽

    四年多码农,毕业后在一家小私企做前端:(初始asp.net,对oo有了比较深切的理解:处于对某空间的效仿,对前端技术架构理解的比较透彻): 在这家公司混了4个月之后跳出来想自己单干: 自己接了个小项目 ...

  10. 贱贱的美团安卓客户端---如何实现让安卓app在应用列表获得较靠前的位置

    起因: 自打愚安我开始使用android设备以来,一直觉得google还算厚道,应用列表里的顺序一直都是依据APP的名称,按照先中文(拼音字母表顺序),后英文(字母表顺序)的原则进行排序的,并没有说G ...