Autocompletetextview is an automatic text box inherited from edittext.
Important attributes:
Android: completionhint
Android: completionthreshold
The test application contains only one simple text box. After a character is entered, the matching string is displayed in the drop-down menu.
When using autocompletetextview, you must match an adapter to provide the content displayed in the drop-down menu.
Final effect:
Program code:
Layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <AutoCompleteTextView android:id="@+id/txt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Type some characters" android:completionThreshold="1" android:popupBackground="#cccccc" /></RelativeLayout>
Java file:
package com.xujin.autocompletetextviewtest;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;public class MainActivity extends Activity {String [] chars = new String[]{"Andy", "Angel Girl","Andrea", "Air Traffic","Akon", "Alan Jackson", "Alison Krauss", "Alizee",};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, chars);AutoCompleteTextView ac =(AutoCompleteTextView)findViewById(R.id.txt);ac.setAdapter(aa);}}