一、实现效果
二、activity_main.ML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.g160628_07.MainActivity">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/actv_main_auto"
android:completionThreshold="1"
/>
</LinearLayout>
三、MainAcctivity.java文件
package com.example.g160628_07;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView actv_main_auto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actv_main_auto = (AutoCompleteTextView) findViewById(R.id.actv_main_auto);
//获取字符串数组(也可以自己定义一个数组)
String data[]=getResources().getStringArray(R.array.data);
//适配器
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);
//给控件设置适配器
actv_main_auto.setAdapter(adapter);
}
}