The Autocompletetextview of Android control development
This control is mainly used when the customer needs to enter some content, will prompt the completion of information for the user to choose, to reduce customer input operations The
selection data in this control can be either a custom string array data or a list data
Java code:
Package com.example.autocompletetext;
Import java.util.ArrayList;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.widget.ArrayAdapter;
Import Android.widget.AutoCompleteTextView;
public class Mainactivity extends Activity {
Private Autocompletetextview Autocompletetextview = null;
The string array is primarily used to match the characters of the client input in the Autocompletetextview object
Private string[] Contry = new string[]{
"Shanghai", "Beijing", "Guangzhou", "Shenzhen", "Nantong", "Anqing", "Anhui",
"Hefei", "Huainan", "Dongguan", "Chuzhou", "Wuxi", "Suzhou", "Nanjing", "Kunshan", "Henan", "Lanfang",
"Liuan", "Huaibei", "Fuyang", "Bozhou", "Suzhou", "Wuhu"
};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Autocompletetextview = (Autocompletetextview) Findviewbyid (r.id.autocomplete);
Creates a ArrayList object and dynamically adds data to the list object. This list is to provide data to the Arrayadapter object
arraylist<string> list = new arraylist<string> ();
List.add ("Testtest");
List.add ("Tetsssss");
Set the adapter adapter, you can use the data in the list, or you can use string array data
arrayadapter<string> adapter = new Arrayadapter<string> (this, R.layout.list_item,/*list*/contry);
To set an adapter for a Autocompletetextview object
Autocompletetextview.setadapter (adapter);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
The
main interface Main.xml code is as follows:
<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"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">
<textview
Android:id= "@+id/mytext"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
<autocompletetextview
Android:id= "@+id/autocomplete"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/mytext"/>
</RelativeLayout>
The
List_itm.xml code is as follows:
This layout file is primarily used to lay out the layout of the pop-up selection list
<?xml version= "1.0" encoding= "Utf-8"?>
<textview xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:padding= "10DP"
Android:textsize= "20SP"
>
</TextView>
The
effect is as follows:
Note: Autocompletetextview needs to enter at least two characters to appear in the list for selection
The Autocompletetextview of Android control development