Android仿百度谷歌搜索自动提示框AutoCompleteTextView简单应用示例
本文实例讲述了Android仿百度谷歌搜索自动提示框AutoCompleteTextView简单应用。分享给大家供大家参考,具体如下:
现在我们上网几乎都会用百度或者谷歌搜索信息,当我们在输入框里输入一两个字后,就会自动提示我们想要的信息,这种效果在Android 里是如何实现的呢? 事实上,Android 的AutoCompleteTextView Widget ,只要搭配ArrayAdapter 就能设计同类似Google 搜索提示的效果.
本例子先在Layout 当中布局一个AutoCompleteTextView Widget ,然后通过预先设置好的字符串数组,将此字符串数组放入ArrayAdapter ,最后利用AutoCompleteTextView.setAdapter 方法,就可以让AutoCompleteTextView 具有自动提示的功能.例如,只要输入ab ,就会自动带出包含ab 的所有字符串列表.
让我们看一下效果图:
下面是我们程序所涉及变动的代码(本例子代码写的相对较少):
首先是main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Please input:" /> <AutoCompleteTextView android:id="@+id/actv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
其次是主控制程序AutoCompleteTextViewDemo.Java:
package com.android.test; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class AutoCompleteTextViewDemo extends Activity { private AutoCompleteTextView actv; private static final String[] autoStrs = new String[]{"a","abc","abcd","abcde","ba"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通过findViewById()方法取到actv actv = (AutoCompleteTextView)findViewById(R.id.actv); //new ArrayAdapter对象并将autoStr字符串数组传入actv中 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,autoStrs); actv.setAdapter(adapter); } }
所有程序就这么一点点哦,大功就这么告成了,最后执行之,将达到上述效果。
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:
- Android高级组件AutoCompleteTextView自动完成文本框使用详解
- Android中EditText和AutoCompleteTextView设置文字选中颜色方法
- Android AutoCompleteTextView控件使用实例
- 基于Android中的 AutoCompleteTextView实现自动填充
- 实例讲解Android中的AutoCompleteTextView自动补全组件
- Android AutoCompleteTextView连接数据库自动提示的方法(附demo源码下载)
- Android自动编辑文本框(AutoCompleteTextView)使用方法详解
- Android中AutoCompleteTextView自动提示
- android中AutoCompleteTextView的简单用法(实现搜索历史)
- Android AutoCompleteTextView控件基本用法示例
- Android开发高级组件之自动完成文本框(AutoCompleteTextView)用法示例【附源码下载】
- Android基础教程(八)之仿百度谷歌搜索自动提示框-----AutoCompleteTextView的应用
- Android基础教程(八)之仿百度谷歌搜索自动提示框-----AutoCompleteTextView的应用
- Android基础教程(八)之仿百度谷歌搜索自动提示框-----AutoCompleteTextView的应用
- Android入门教程(十二)之仿百度谷歌搜索自动提示框-----AutoCompleteTextView的应用(转)
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android深入浅出系列之实例应用—具有自动提示的菜单应用AutoCompleteTextView和ArrayAdapter的使用
- Android 应用开发笔记 - 自动提示(AutoComplete-TextView) & ArrayAdapter
- Android深入浅出系列之实例应用—具有自动提示的菜单应用AutoCompleteTextView和ArrayAdapter的使用
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android深入浅出系列之实例应用—具有自动提示的菜单应用AutoCompleteTextView和ArrayAdapter的使用
- Android 利用AutoCompleteTextView实现模糊搜索功能,搜索结果自动提示,识别拼音首字母并转汉字提示
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- Android自动提示--AutoCompleteTextView、MultiAutoCompleteTextView
- Android自动提示文本框(AutoCompleteTextView)
- 安卓编程——自动提示框AutoCompleteTextView的应用
- Android:控件AutoCompleteTextView 自动提示