---
title: "EditText输入框自动提示功能"
author: "logger"
date: "2010-12-05T21:08:12Z"
tags: []
categories: ["移动开发"]
slug: "edittext"
draft: false
lastmod: "2010-12-05T21:08:12Z"
id: 834012
url: "https://logger.iteye.com/blog/834012"
---

所示的图片中输入as前两个字母，后面就会提示相应的结果提示，这在apidemo中也有类似例子的提供，网上传的也基本都是那个例子。
  这个功能用处不大，但是在于新意不错，说专业一点就是用户体验不错。
  每个事情都有缺点，这个的坏处就是必须输入两个以上的字符，两个！
  实现此功能的是EditText的子类控件AutoCompleteTextView。既是子类，所以有着全部父类的属性，即用法与EditText相同。（MultiAutoCompleteTextView这个的属性更加强大，有兴趣的朋友可以继续研究）。
1。定义xml
<AutoCompleteTextView
    android:id="@+id/et1"
    android:hint="软件,游戏,尽在96商城"       // 此处为自己定义的属性，可不要。提示信息
    android:layout_width="220px"
    android:layout_height="50px"
    android:layout_marginLeft="30px"
   >
2。在Activity中定义相对应的属性来配合xml显示。

public class Enter extends Activity {

   String result_about_a = {"andexplorer","astro"};
         private AutoCompleteTextView field_text;    （定义AutoCompleteTextView对象）

public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);


	      field_text = (AutoCompleteTextView) findViewById(R.id.et1);
		//自动提示
		ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line,
				result_about_a);
		field_text.setAdapter(adapt);

}
}

但是，此方法的一个最大不好处就是上面的数组自定义。这个简直就是愚蠢的做法，应该可以在创建一个数据库用来存放一些被搜索的内容，然后等用到的时候再用Adapter来相应的对应上，但是自己还没那样做。还是惰性太大啊。希望有此经验的朋友有机会可以给我一些建议。共同学习。共同进步，