---
title: "AutoCompleteText"
author: "AndLi"
date: "2012-02-28T09:23:55Z"
tags: []
categories: ["移动开发"]
slug: "autocompletetext"
draft: false
lastmod: "2012-03-20T21:51:12Z"
id: 1428579
url: "https://andli.iteye.com/blog/1428579"
---

```
ArrayAdapter<String> adapter = new ArrayAdapter<String>(// 创建适配器
				this, // Context
				android.R.layout.simple_dropdown_item_1line,// 布局
				array);// 资源数组
		AutoCompleteTextView myAutoCompleteTextView = // 得到控件的引用
		(AutoCompleteTextView) findViewById(R.id.myAutoCompleteTextView);
		myAutoCompleteTextView.setAdapter(adapter);// 设置适配器
```

 

 

 

 

```
 <AutoCompleteTextView
                    android:id="@+id/qr_tv_road"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/white"
                    android:textSize="18sp" />
```

 

 

 

 

```
	private void initRoadInfo() {
		roadname_atv.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String defaultRoad = roadname_atv.getText().toString().trim();
				String fulltext = CodeItemDBHelp.getFullText(
						QWT_QuestionReportView.this, defaultRoad,
						getResources().getString(R.string.ld_pda));
				if (!TextUtils.isEmpty(fulltext) && !fulltext.equals("null")) {
					qr_address_et.setText(fulltext);
				} else {
					qr_address_et.setText(defaultRoad);
				}
				LogHelp.LogI("onTextChanged");

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				String defaultRoad = roadname_atv.getText().toString().trim();
				String fulltext = CodeItemDBHelp.getFullText(
						QWT_QuestionReportView.this, defaultRoad,
						getResources().getString(R.string.ld_pda));
				if (!TextUtils.isEmpty(fulltext) && !fulltext.equals("null")) {
					qr_address_et.setText(fulltext);
				} else {
					qr_address_et.setText(defaultRoad);
				}
				LogHelp.LogI("beforeTextChanged");

			}

			@Override
			public void afterTextChanged(Editable s) {
				String defaultRoad = roadname_atv.getText().toString().trim();
				String fulltext = CodeItemDBHelp.getFullText(
						QWT_QuestionReportView.this, defaultRoad,
						getResources().getString(R.string.ld_pda));
				if (!TextUtils.isEmpty(fulltext) && !fulltext.equals("null")) {
					qr_address_et.setText(fulltext);
				} else {
					qr_address_et.setText(defaultRoad);
				}
				DBHelp.setConfigValue(QWT_QuestionReportView.this,
						ConfigKey.defaultroad, defaultRoad);// 配置默认路段
				LogHelp.LogI("afterTextChanged");

			}
		});
		List<CodeItem> codeItems = CodeItemDBHelp.getCodeItemListByCodeID(this,
				getResources().getString(R.string.ld_pda));
		if (codeItems.size() > 0) {
			String text = "";
			for (int i = 0; i < codeItems.size(); i++) {
				text += codeItems.get(i).getItemtext() + ";";
			}
			// LogHelp.LogI(text);
			String array[] = text.split(";");
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_dropdown_item_1line, array);// 资源数组
			roadname_atv.setAdapter(adapter);// 设置适配器
			roadname_atv.setThreshold(0);// 定义需要用户输入的字符数
		}

	}
```