First, add the AutoCompleteTextView component to the layout file main. xml. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<AutoCompleteTextView
Android: id = "@ + id/auto_complete"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</LinearLayout>
Then define the data to be bound in MainActivity. java and bind it to the component. The Code is as follows:
Public class MainActivity extends Activity {
// Define a set of data to be bound
Private static final String [] COUNTRIES = new String [] {"China", "Russia ",
"Germany", "USA", "Chinal", "Germany1", "China12 "};
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Bind data
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_dropdown_item_1line, COUNTRIES );
AutoCompleteTextView textview = (AutoCompleteTextView) findViewById (R. id. auto_complete );
Textview. setAdapter (adapter );
}
}
As follows:
This article is from the "Android Learning Experience" blog