【Android】自动提示匹配之AutoCompleteTextView
public class
AutoCompleteTextView
extendsEditText
implementsFilter.FilterListener
java.lang.Object ↳ android.view.View ↳ android.widget.TextView ↳ android.widget.EditText ↳ android.widget.AutoCompleteTextView
An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
简单的讲一个类似百度,google等输入搜索字串时自动提示的功能:
常用方法总结
Public Methods <nobr><a href="http://developer.android.com/reference/android/widget/ListAdapter.html" style="color:rgb(0,102,153); text-decoration:none">ListAdapter</a></nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#getAdapter()" style="color:rgb(0,102,153); text-decoration:none">getAdapter</a></span>()</nobr>
Returns a filterable list adapter used for auto completion.
<nobr><a href="http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html" style="color:rgb(0,102,153); text-decoration:none">AdapterView.OnItemClickListener</a></nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#getItemClickListener()" style="color:rgb(0,102,153); text-decoration:none">getItemClickListener</a></span>()</nobr>
This method is deprecated. UsegetOnItemClickListener()intead
<nobr><a href="http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html" style="color:rgb(0,102,153); text-decoration:none">AdapterView.OnItemSelectedListener</a></nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#getItemSelectedListener()" style="color:rgb(0,102,153); text-decoration:none">getItemSelectedListener</a></span>()</nobr>
This method is deprecated. UsegetOnItemSelectedListener()intead
<nobr><a href="http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html" style="color:rgb(0,102,153); text-decoration:none">AdapterView.OnItemClickListener</a></nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#getOnItemClickListener()" style="color:rgb(0,102,153); text-decoration:none">getOnItemClickListener</a></span>()</nobr>
Returns the listener that is notified whenever the user clicks an item in the drop down list.
<nobr><a href="http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html" style="color:rgb(0,102,153); text-decoration:none">AdapterView.OnItemSelectedListener</a></nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#getOnItemSelectedListener()" style="color:rgb(0,102,153); text-decoration:none">getOnItemSelectedListener</a></span>()</nobr>
Returns the listener that is notified whenever the user selects an item in the drop down list.
<nobr>int</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#getThreshold()" style="color:rgb(0,102,153); text-decoration:none">getThreshold</a></span>()</nobr>
Returns the number of characters the user must type before the drop down list is shown.
<nobr><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.Validator.html" style="color:rgb(0,102,153); text-decoration:none">AutoCompleteTextView.Validator</a></nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#getValidator()" style="color:rgb(0,102,153); text-decoration:none">getValidator</a></span>()</nobr>
Returns the Validator set withsetValidator(AutoCompleteTextView.Validator), ornullif it was not set.
<nobr>boolean</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#isPerformingCompletion()" style="color:rgb(0,102,153); text-decoration:none">isPerformingCompletion</a></span>()</nobr>
Identifies whether the view is currently performing a text completion, so subclasses can decide whether to respond to text changed events.
<nobr>boolean</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#isPopupShowing()" style="color:rgb(0,102,153); text-decoration:none">isPopupShowing</a></span>()</nobr>
Indicates whether the popup menu is showing.
<nobr>void</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#onCommitCompletion(android.view.inputmethod.CompletionInfo)" style="color:rgb(0,102,153); text-decoration:none">onCommitCompletion</a></span>(<a href="http://developer.android.com/reference/android/view/inputmethod/CompletionInfo.html" style="color:rgb(0,102,153); text-decoration:none">CompletionInfo</a>completion)</nobr>
Called by the framework in response to a text completion from the current input method, provided by it callingInputConnection.commitCompletion().
<nobr>void</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#onFilterComplete(int)" style="color:rgb(0,102,153); text-decoration:none">onFilterComplete</a></span>(int count)</nobr>
Notifies the end of a filtering operation.
<nobr>boolean</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#onKeyDown(int,%20android.view.KeyEvent)" style="color:rgb(0,102,153); text-decoration:none">onKeyDown</a></span>(int keyCode,<a href="http://developer.android.com/reference/android/view/KeyEvent.html" style="color:rgb(0,102,153); text-decoration:none">KeyEvent</a>event)</nobr>
Default implementation ofKeyEvent.Callback.onKeyDown(): perform press of the view whenKEYCODE_DPAD_CENTERorKEYCODE_ENTERis released, if the view is enabled and clickable.
<nobr>boolean</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#onKeyUp(int,%20android.view.KeyEvent)" style="color:rgb(0,102,153); text-decoration:none">onKeyUp</a></span>(int keyCode,<a href="http://developer.android.com/reference/android/view/KeyEvent.html" style="color:rgb(0,102,153); text-decoration:none">KeyEvent</a>event)</nobr>
Default implementation ofKeyEvent.Callback.onKeyUp(): perform clicking of the view whenKEYCODE_DPAD_CENTERorKEYCODE_ENTERis released.
<nobr>void</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#onWindowFocusChanged(boolean)" style="color:rgb(0,102,153); text-decoration:none">onWindowFocusChanged</a></span>(boolean hasWindowFocus)</nobr>
Called when the window containing this view gains or loses focus.
<nobr><Textends<a href="http://developer.android.com/reference/android/widget/ListAdapter.html" style="color:rgb(0,102,153); text-decoration:none">ListAdapter</a>&<a href="http://developer.android.com/reference/android/widget/Filterable.html" style="color:rgb(0,102,153); text-decoration:none">Filterable</a>> void</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#setAdapter(T)" style="color:rgb(0,102,153); text-decoration:none">setAdapter</a></span>(T adapter)</nobr>
Changes the list of data used for auto completion.
<nobr>void</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#setCompletionHint(java.lang.CharSequence)" style="color:rgb(0,102,153); text-decoration:none">setCompletionHint</a></span>(<a href="http://developer.android.com/reference/java/lang/CharSequence.html" style="color:rgb(0,102,153); text-decoration:none">CharSequence</a>hint)</nobr>
Sets the optional hint text that is displayed at the bottom of the the matching list.
<nobr>void</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#setDropDownBackgroundResource(int)" style="color:rgb(0,102,153); text-decoration:none">setDropDownBackgroundResource</a></span>(int id)</nobr>
Sets the background of the auto-complete drop-down list.
<nobr>void</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#setOnClickListener(android.view.View.OnClickListener)" style="color:rgb(0,102,153); text-decoration:none">setOnClickListener</a></span>(<a href="http://developer.android.com/reference/android/view/View.OnClickListener.html" style="color:rgb(0,102,153); text-decoration:none">View.OnClickListener</a>listener)</nobr>
Register a callback to be invoked when this view is clicked.
<nobr>void</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)" style="color:rgb(0,102,153); text-decoration:none">setOnItemClickListener</a></span>(<a href="http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html" style="color:rgb(0,102,153); text-decoration:none">AdapterView.OnItemClickListener</a>l)</nobr>
Sets the listener that will be notified when the user clicks an item in the drop down list.
<nobr>void</nobr> <nobr><span class="sympad" style="margin-right:2px"><a href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)" style="color:rgb(0,102,153); text-decoration:none">setOnItemSelectedListener</a></span>(<a href="http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html" style="color:rgb(0,102,153); text-decoration:none">AdapterView.OnItemSelectedListener</a>l)</nobr>
Sets the listener that will be notified when the user selects an item in the drop down list.
下面是一个官方的示例:
To create a text entry widget that provides auto-complete suggestions, use theAutoCompleteTextViewwidget. Suggestions are received from a collection of strings associated with the widget through anArrayAdapter.
In this tutorial, you will create aAutoCompleteTextViewwidget that provides suggestions for a country name.
【0】Start a new project namedHelloAutoComplete.
【1】Create an XML file namedlist_item.xmland save it inside theres/layout/folder. Edit the file to look like this:
This file defines a simpleTextViewthat will be used for each item that appears in the list of suggestions.
这个文件定义了一个简单的TextView,用来即时显示自动匹配的选项
【2】Open theres/layout/main.xmlfile and insert the following:
TheTextViewis a label that introduces theAutoCompleteTextViewwidget.
【3】OpenHelloAutoComplete.javaand insert the following code for theonCreate()method:
After the content view is set to themain.xmllayout, theAutoCompleteTextViewwidget is captured from the layout withfindViewById(int). A newArrayAdapteris then initialized to bind thelist_item.xmllayout to each list item in theCOUNTRIESstring array (defined in the next step). Finally,setAdapter()is called to associate theArrayAdapterwith theAutoCompleteTextViewwidget so that the string array will populate the list of suggestions.
这里new了一个ArrayAdapter用来作为adapter.
【4】Inside theHelloAutoCompleteclass, add the string array:
This is the list of suggestions that will be provided in a drop-down list when the user types into theAutoCompleteTextViewwidget.
【5】Run the application.

More Information
Note that using a hard-coded string array is not a recommended design practice because your application code should focus on behavior, not content. Application content such as strings should be externalized from the code in order to make modifications to the content easier and facilitate localization of the content. The hard-coded strings are used in this tutorial only to make it simple and focus on theAutoCompleteTextViewwidget. Instead, your application should declare such string arrays in an XML file. This can be done with a<string-array<resource in your projectres/values/strings.xmlfile. For example:
这里讲hard-coded不利于国际化,我们做的AP应该focus在behavior上而不是content.所以建议把上面的String-array放在String.xml文件中,例如:
To use these resource strings for theArrayAdapter, replace the originalArrayAdapterconstructor line with the following:
翻译自:http://developer.android.com/resources/tutorials/views/hello-autocomplete.html
