原文链接:http://blog.csdn.net/android_tutor/article/details/4831552


现在我们上网几乎都会用百度或者谷歌搜索信息,当我们在输入框里输入一两个字后,就会自动提示我们想要的信息,这种效果在Android 里是如何实现的呢? 事实上,Android 的AutoCompleteTextView Widget ,只要搭配ArrayAdapter 就能设计同类似Google 搜索提示的效果.

 

本例子先在Layout 当中布局一个AutoCompleteTextView Widget ,然后通过预先设置好的字符串数组,将此字符串数组放入ArrayAdapter ,最后利用AutoCompleteTextView.setAdapter 方法,就可以让AutoCompleteTextView 具有自动提示的功能.例如,只要输入ab ,就会自动带出包含ab 的所有字符串列表.

 

让我们看一下效果图:

 

Android开发之自动提示框—AutoCompleteTextView_第1张图片

 

下面是我们程序所涉及变动的代码(本例子代码写的相对较少):

 

首先是main.xml:

 

<?xml version="1.0" encoding="utf-8"?>
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Please input:" 
    />
    android:id="@+id/actv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>
 

其次是主控制程序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 adapter = new ArrayAdapter(this,
                android.R.layout.simple_dropdown_item_1line,autoStrs);   
        actv.setAdapter(adapter);   

    }
}


更多相关文章

  1. Android 出错提示:Emulator without GPU emulation detected 的解
  2. Android Studio Termanal打不开,提示java.io.IOEXception:couldn'
  3. TextInputEditText 替换EditText解决警告Missing autofillHints
  4. android ndk ant build提示 taskdef class com.android.ant.Setu
  5. Android Studio高级控件(自动提示文本框)
  6. Android 实现任务栏出现类似短信的提示功能
  7. Android Studio - 第四十二期 几个经常用到的字符串的截取
  8. Android studio使用过程中提示“警告: [deprecation] android.ha
  9. 字符串资源多国语言版本的出错问题

随机推荐

  1. Android API 中文 (112) —— ThumbnailU
  2. editView多行光标位置问题和联系人问题
  3. Android(安卓)2.3.3 NFC分析
  4. Android 之 ImageSwitch图片转换器
  5. Android 使用 第三方 Sweet Alert Dialog
  6. Android Camera 方法分析
  7. Android NDK学习(1) 简介
  8. EditText 所有属性
  9. Edittext设置输入属性,包括使用代码设置
  10. 】Android横竖屏总结