Android provides two types of smart input boxes: AutoCompleteTextView and MultiAutoCompleteTextView. Their functions are roughly the same. The display effect is the same as that in Google search. When you enter some characters in the search box (at least two characters), a drop-down box will pop up prompting similar results. The following describes in detail.
I. AutoCompleteTextView
1. Introduction
An inherited fromEditView
Of
The editable text view can dynamically match the input content. For example, when google search engine inputs text, it can display matching hot information based on the content.
2. Important Methods
ClearListSelection (): clears the selected list items.
DismissDropDown (): If the drop-down menu is closed
GetAdapter (): Get the adapter
Ii. MultiAutoCompleteTextView
1. Introduction
An inherited fromAutoCompleteTextView
Of
The editable text view effectively expands the text you type without entering the entire content. (If you enter a part of the content, the system will prompt the remaining part of the content ).
The user must provideMultiAutoCompleteTextView.Tokenizer
Used to distinguish different substrings.
2. Important Methods
EnoughToFilter (): filter when the text length exceeds the threshold.
Validate mvalidation (): instead of verifying the entire text, this subclass method verifies each individual text mark
SetTokenizer (MultiAutoCompleteTextView. Tokenizer t): When the user is inputting, The tokenizer setting is used to determine the range of Text
The following are examples
Main. xml layout File
<?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/autoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
TextViewActivity class
Package com. ljq. activity;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. ArrayAdapter;
Import android. widget. AutoCompleteTextView;
Import android. widget. MultiAutoCompleteTextView;
Public class TextViewActivity extends Activity {
// Jiudi city, Fujian Province
Private static final String [] cities = new String []
{"FuZhou", "XiaMen", "NiDe", "PuTian", "QuanZhou", "ZhangZhou", "LongYan", "SanMing", "NanPing "};
Private AutoCompleteTextView autoCompleteTextView = null;
Private MultiAutoCompleteTextView multiAutoCompleteTextView = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
AutoCompleteTextView = (AutoCompleteTextView) findViewById (R. id. autoCompleteTextView );
MultiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById (R. id. multiAutoCompleteTextView );
// Create an adapter
ArrayAdapter <String> adapter = new ArrayAdapter <String> (
This,
Android. R. layout. simple_dropdown_item_1line,
Cities );
AutoCompleteTextView. setAdapter (adapter );
// Set the number of characters entered and prompt, the default value is 2
AutoCompleteTextView. setThreshold (2 );
MultiAutoCompleteTextView. setAdapter (adapter );
MultiAutoCompleteTextView. setThreshold (2 );
// You must provide a MultiAutoCompleteTextView. Tokenizer to distinguish different substrings.
MultiAutoCompleteTextView. setTokenizer (new MultiAutoCompleteTextView. CommaTokenizer ());
}
}
Running result