首先是布局文件activity_auto_complete.xml:

<?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"
android:orientation="vertical"
tools:context=".AutoCompleteActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:"
android:textSize="25sp" />

<AutoCompleteTextView
android:id="@+id/auto_tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:hint="请输入您的爱好" />
</LinearLayout>

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="获取文本框中的值"
android:textSize="20sp" />

</LinearLayout>

之后在res/values目录下创建array.xml资源文件

<resources>
<string-array name="hobby">
<item>读书</item>
<item>跑步</item>
<item>唱歌</item>
<item>旅游</item>
<item>写代码</item>
<item>写字</item>
<item>篮球</item>
<item>乒乓球</item>
</string-array>
</resources>

之后在AutoCompleteActivity文件中通过系统提供的数组适配器ArrayAdapter,显示数据。

public class AutoCompleteActivity extends AppCompatActivity {
private AutoCompleteTextView auto_tv1;
private ArrayAdapter arrayAdapter;
private String[] data = new String[]{"读书", "跑步", "唱歌", "旅游", "写代码", "篮球", "乒乓球"};
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_complete);
auto_tv1 = findViewById(R.id.auto_tv1);
btn = findViewById(R.id.btn);


// arrayAdapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item,data );
arrayAdapter = ArrayAdapter.createFromResource(this,R.array.hobby,R.layout.support_simple_spinner_dropdown_item);
auto_tv1.setAdapter(arrayAdapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(AutoCompleteActivity.this, auto_tv1.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}

效果图显示:

Android 自动补齐文本框AutoCompleteTextView的使用_android