package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import java.lang.reflect.Array;
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView acTextView;
private String[] res={"beijing1","beijing2","beijing3","shanghai1","shanghai2"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1:初始化控件
acTextView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
//2:需要一个适配器
//3:初始化一个数据源去匹配文本框输入的内容
ArrayAdapter<String> adapter=new ArrayAdapter<>(this ,android.R.layout.simple_list_item_1,res);
//4:将adapter与AutoCompleteTextView绑定
acTextView.setAdapter(adapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:completionThreshold="3"
android:hint="请输入你搜索的关键词" />
</LinearLayout>