Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- <?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="wrap_content"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <AutoCompleteTextView
- android:id="@+id/auto"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:completionHint="最近5条记录"
- android:completionThreshold="1"
- />
- <Button
- android:id="@+id/search"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="搜索" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <Button
- android:id="@+id/clean"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="清除历史记录"
- android:onClick="cleanHistory"
- />
- </LinearLayout>
- </LinearLayout>
- public class TestActivity extends Activity {
- private AutoCompleteTextView auto;
- private Button searchbtn;
- private ArrayAdapter<String> arr_adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.test);
- // 初始化
- auto = (AutoCompleteTextView) findViewById(R.id.auto);
- searchbtn = (Button) findViewById(R.id.search);
- // 获取搜索记录文件内容
- SharedPreferences sp = getSharedPreferences("search_history", 0);
- String history = sp.getString("history", "暂时没有搜索记录");
- // 用逗号分割内容返回数组
- String[] history_arr = history.split(",");
- // 新建适配器,适配器数据为搜索历史文件内容
- arr_adapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_dropdown_item_1line, history_arr);
- // 保留前50条数据
- if (history_arr.length > 50) {
- String[] newArrays = new String[50];
- // 实现数组之间的复制
- System.arraycopy(history_arr, 0, newArrays, 0, 50);
- arr_adapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_dropdown_item_1line, history_arr);
- }
- // 设置适配器
- auto.setAdapter(arr_adapter);
- // 设置监听事件,点击搜索写入搜索词
- searchbtn.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- save();
- }
- });
- }
- public void save() {
- // 获取搜索框信息
- String text = auto.getText().toString();
- SharedPreferences mysp = getSharedPreferences("search_history", 0);
- String old_text = mysp.getString("history", "暂时没有搜索记录");
- // 利用StringBuilder.append新增内容,逗号便于读取内容时用逗号拆分开
- StringBuilder builder = new StringBuilder(old_text);
- builder.append(text + ",");
- // 判断搜索内容是否已经存在于历史文件,已存在则不重复添加
- if (!old_text.contains(text + ",")) {
- SharedPreferences.Editor myeditor = mysp.edit();
- myeditor.putString("history", builder.toString());
- myeditor.commit();
- Toast.makeText(this, text + "添加成功", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(this, text + "已存在", Toast.LENGTH_SHORT).show();
- }
- }
- //清除搜索记录
- public void cleanHistory(View v){
- SharedPreferences sp =getSharedPreferences("search_history",0);
- SharedPreferences.Editor editor=sp.edit();
- editor.clear();
- editor.commit();
- Toast.makeText(this, "清除成功", Toast.LENGTH_SHORT).show();
- super.onDestroy();
- }
- }
相关文章:
Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示的更多相关文章
- Android——控件AutoCompleteTextView 自动提示
Android:控件AutoCompleteTextView 自动提示 在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextV ...
- Android 控件 -------- AutoCompleteTextView 动态匹配内容,例如 百度搜索提示下拉列表功能
AutoCompleteTextView 支持基本的自动完成功能,适用在各种搜索功能中,并且可以根据自己的需求设置他的默认显示数据.两个控件都可以很灵活的预置匹配的那些数据,并且可以设置输入多少值时开 ...
- Android控件——AutoCompleteTextView与MultiAutoCompleteTextView(实现自动匹配输入的内容)
------------------------------------AutoCompleteTextView----------------------
- Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度
Android AutoCompleteTextView 控件实现类似被搜索提示,效果如下 1.首先贴出布局代码 activity_main.xml: <?xml version="1 ...
- 一步一步学android控件(之六) —— MultiAutoCompleteTextView
今天学习的控件是MultiAutoCompleteTextView . 提到MultiAutoCompleteTextView 我们就自然而然地想到AutoCompleteTextView ,就想知道 ...
- 从Android系统出发,分析Android控件构架
从Android系统出发,分析Android控件构架 Android中所有的控件追溯到根源,就是View 和ViewGroup,相信这个大家都知道,但是大家也许会不太清楚它们之间的具体关系是什么,在A ...
- android控件的属性
android控件的属性 本节描述android空间的位置,内容等相关属性及属性的含义 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ( ...
- Robotium之Android控件定位实践和建议(Appium/UIAutomator姊妹篇)
本人之前以前撰文描写叙述Appium和UIAutomator框架是怎样定位Android界面上的控件的. UIAutomator定位Android控件的方法实践和建议 Appium基于安卓的各种Fin ...
- Robotium之Android控件定位实践和建议
本人之前曾经撰文描述Appium和UIAutomator框架是如何定位Android界面上的控件的. UIAutomator定位Android控件的方法实践和建议Appium基于安卓的各种FindEl ...
随机推荐
- 11g RAC R2 体系结构---用户及用户组
10.2 RAC 到11.2 RAC 用户及用户组的变化: 在10.2 RAC 的部署中,只需要一个用户(oracle)和一个用户组(dba).Database.Clusterware都是用oracl ...
- 客户端访问WebService和PageMethod
客户端访问WebService 客户端访问WebService和后台访问WebService没什么不同,注意的地方是要在ScriptManager中添加 <Services> ...
- mac os使用homebrew来管理后台服务
在linux下我们经常通过 service 或者 /etc/init.d/来管理我们的后台服务软件,并使用包管理器安装这些软件. 在mac下有homebrew这个好用的工具来安装软件,但是一直没有找到 ...
- objdump的使用方法和 symbol table的每列的含义
一.objdump的用法 objdump命令的man手册 objdump [-a] [-b bfname| --target=bfdname] [-C] [--debugging] ...
- django 更新model
修改models.py 中对应的class 在admin.py 中 增加 admin.site.register(WafDevice) 进入dbshell python manage.py dbshe ...
- Ubuntu首次开启root用户
最近一直在学习linux,选择ubuntu作为联系的操作系统.然后一直发现自己所创建的用户和root用户不是一个概念,执行好多命令的时候都提示没有权限.这样,最后终于发现原来是ubuntu是默认关闭r ...
- 扒一扒各大电商网站的m站都用的什么前端技术输入日志标题
凡客首页使用Swiper和zepto,没有使用jquery , 静态首页+js交互, 资源加载使用 lazyLoad X-AspNet-Version: 4.0.30319 X-AspNetMvc- ...
- 屏蔽ios7中某个页面的默认手势滑回返回
- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:YES];self.navigationController.in ...
- 好项目烂架构的问题,四年coder的吐槽
四年多码农,毕业后在一家小私企做前端:(初始asp.net,对oo有了比较深切的理解:处于对某空间的效仿,对前端技术架构理解的比较透彻): 在这家公司混了4个月之后跳出来想自己单干: 自己接了个小项目 ...
- 贱贱的美团安卓客户端---如何实现让安卓app在应用列表获得较靠前的位置
起因: 自打愚安我开始使用android设备以来,一直觉得google还算厚道,应用列表里的顺序一直都是依据APP的名称,按照先中文(拼音字母表顺序),后英文(字母表顺序)的原则进行排序的,并没有说G ...