Now we will use Baidu or Google to search for information on the Internet. When we enter one or two words in the input box, we will automatically prompt the information we want.AndroidYesAndroidOfAutoCompleteTextView WidgetMatchingArrayAdapterSimilar DesignGoogleSearch for the effect of the prompt.
First inLayoutLayout oneAutoCompleteTextView WidgetAnd then put this string array intoArrayAdapter, And finally useAutoCompleteTextView. setAdapterMethod, you can makeAutoCompleteTextViewProvides the automatic prompt function. For example, you only need to enterABWill automatically includeABList of all strings.Copy codeThe Code is as follows: public class MainActivity extends Activity {
Private AutoCompleteTextView actv;
Private static final String [] autoStrs = new String [] {"a", "abc", "abcde "};
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Actv = (AutoCompleteTextView) findViewById (R. id. actv );
ArrayAdapter adapter = new ArrayAdapter (this, android. R. layout. simple_dropdown_item_1line, autoStrs );
Actv. setAdapter (adapter );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
}
Copy codeThe Code is as follows: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<AutoCompleteTextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/actv"
Android: completionThreshold = "1"/> <! -- Set to start matching only by entering one word. -->
</LinearLayout>