AutoCompleteTextView
来源:互联网 发布:cf近战武器数据 编辑:程序博客网 时间:2024/04/29 09:59
在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextView实现的。
继承关系:
public class AutoCompleteTextView extends EditText implements FilterListener {...}public class EditText extends TextView {...}
基本使用:
* XML布局:*
<AutoCompleteTextView android:id="@+id/autotext" android:layout_width="match_parent" android:layout_height="wrap_content"/>
Java代码:
AutoCompleteTextView autotext = (AutoCompleteTextView) findViewById(R.id.autotext); String[] arr = {"aa", "aab", "aac","bb","baa","bac"}; ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr); autotext.setAdapter(arrayAdapter);
AutoCompleteTextView常用属性:
android:completionHint //设置出现在下拉菜单中的提示标题android:completionThreshold //设置用户至少输入多少个字符才会显示提示// 默认是从第二个字符开始匹配的 //如果设置输入第一个字符就进行提示//加入下面这行代码autotext.setThreshold(1); android:dropDownHorizontalOffset //下拉菜单于文本框之间的水平偏移。默认与文本框左对齐android:dropDownHeight //下拉菜单的高度android:dropDownWidth //下拉菜单的宽度android:singleLine //单行显示android:dropDownVerticalOffset //垂直偏移量
保存本地并自动提示搜索:
xml文件:
<?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/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:onClick="cleanHistory" android:text="清除历史记录" /> </LinearLayout></LinearLayout>
Java代码:
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);//System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制。//其函数原型是: public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) //src:源数组; srcPos:源数组要复制的起始位置; dest:目的数组; destPos:目的数组放置的起始位置; length:复制的长度。 //注意:src and dest都必须是同类型或者可以进行转换类型的数组. 有趣的是这个函数可以实现自己到自己复制,//比如: int[] fun ={0,1,2,3,4,5,6}; System.arraycopy(fun,0,fun,3,3); //则结果为:{0,1,2,0,1,2,6}; 实现过程是这样的,先生成一个长度为length的临时数组,//将fun数组中srcPos 到srcPos+length-1之间的数据拷贝到临时数组中,再执行System.arraycopy(临时数组,0,fun,3,3). 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) { 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 客户端保存搜索历史自动提示
使用System.arraycopy()实现数组之间的复制
0 0
- AutoCompleteTextView
- AutoCompleteTextView
- AutoCompleteTextView
- AutoCompleteTextView
- AutoCompleteTextView
- AutoCompleteTextView
- AutoCompleteTextView
- AutoCompleteTextView
- autocompletetextview
- AutoCompleteTextView
- autocompletetextview
- AutoCompleteTextView
- AutoCompleteTextView
- AutoCompleteTextView
- AutocompleteTextView
- AutoCompleteTextView
- AutoCompleteTextView
- AutoCompleteTextView
- 从最初的看客到自己退役 一个渣ACMer的感想
- 收到银行承兑汇票应该注意什么
- 关于AB打包-加载
- odbcConnectExcel()只支持32位的操作系统
- junit下载地址
- AutoCompleteTextView
- 欢迎使用CSDN-markdown编辑器
- 嵌入式linux之Uboot和系统移植--基础
- JdbcDaoSupport操作
- Tesseract-OCR入门使用(1)-安装包获取和命令行调用
- C语言程序设计---8:指针
- JDK并发包---(8)ReadWriteLock读写锁:替换ReentrantLock,提高性能
- Unity3D实现Tab切换,页面切换功能
- 一不小心修改了/etc/profile 文件导致命令不可用怎么办,不要怕~