当前位置:操作系统 > 安卓/Android >>

Android ApiDemos示例解析(99):Views->Auto Complete->1. Screen Top

本例 1. Screen Top ,2. Screen Bottom , 3. Scroll 都是介绍AutoCompleteTextView 的用法,不同的是AutoCompleteTextView 中Layout 位置不同,可以看到 AutoCompleteTextView 根据其位置的不同自动为提示对话框会选择合适的位置在屏幕上显示。

在使用Google搜索时,Google 搜索项会随着用户输入自动给出相关提示,类AutoCompleteTextView 提供了类似的功能,在AutoCompleteTextView输入时,随着用户的输入,AutoCompleteTextView会显示一个提示列表可以供用户选择,用户可以使用Back 键随时取消这个提示框。

提示框的内容来自于一个Data Adapter ,并可以通过设定 threshold 给出用户输入超过几个字符后才出现提示框。

本例为提示框添加国家名称的提示:

[java]
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
 android.R.layout.simple_dropdown_item_1line, 
 COUNTRIES); 
AutoCompleteTextView textView 
 = (AutoCompleteTextView) findViewById(R.id.edit); 
textView.setAdapter(adapter); 
 
... 
static final String[] COUNTRIES = new String[] { 
 "Afghanistan", "Albania", "Algeria", "American Samoa", 
 "Andorra",    "Angola", "Anguilla", "Antarctica", 
 "Antigua and Barbuda", "Argentina" .. 

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
 android.R.layout.simple_dropdown_item_1line,
 COUNTRIES);
AutoCompleteTextView textView
 = (AutoCompleteTextView) findViewById(R.id.edit);
textView.setAdapter(adapter);

...
static final String[] COUNTRIES = new String[] {
 "Afghanistan", "Albania", "Algeria", "American Samoa",
 "Andorra",    "Angola", "Anguilla", "Antarctica",
 "Antigua and Barbuda", "Argentina" ..

本例的AutoCompleteTextView 位置在屏幕的上部,可以看看提示框显示的位置:

 \
作者:mapdigit
 

 


补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,