In this chapter, we will introduce the AutoCompleteTextView and MultiAutoCompleteTextView controls, both of which are the child pieces of EditText. What is the purpose?
Let's take the monsters from the previous chapters of the Journey to the West to capture Tang Miao and migrate them. Monsters say they have too many people and I cannot remember their names. It seems that one of them is from pig. No, what should I do if two monsters are arguing? It doesn't matter. This uses our automatic prompt control. When the pig is entered, let's look at the name of the Bajie. In this case, we don't need to argue. It's easy.
But what is the difference between the two? Look at the name. The AutoCompleteTextView control selects one at a time and one at a time. the MultiAutoCompleteTextView control is used to select a few widgets at a time, but one widget is not needed at a time. okay. Let's see.
Main. xml
View plain
<? 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">
<TextView android: text = "this time the monsters want to capture" android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </TextView>
<AutoCompleteTextView android: layout_height = "wrap_content"
Android: id = "@ + id/autoCompleteTextView" android: layout_width = "match_parent"
Android: hint = "enter a travel character you know" android: completionHint = "my characters"
Android: completionThreshold = "1">
</AutoCompleteTextView>
<MultiAutoCompleteTextView android: id = "@ + id/multiAutoCompleteTextView"
Android: layout_height = "wrap_content" android: layout_width = "match_parent"
Android: hint = "enter a travel character you know" android: completionHint = "my characters"
Android: completionThreshold = "1"> </MultiAutoCompleteTextView>
<TextView android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "@ string/hello"
Android: id = "@ + id/text"> </TextView>
</LinearLayout>
Two controls are defined, one is the AutoCompleteTextView control and the other is the MultiAutoCompleteTextView control.
The java code of the Activity is as follows:
View plain
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemClickListener;
Import android. widget. ArrayAdapter;
Import android. widget. AutoCompleteTextView;
Import android. widget. MultiAutoCompleteTextView;
Import android. widget. TextView;
Public class ButtonDemoActivity extends Activity
{
Private TextView text = null;
Private String [] item = {"Tang Seng", "Sun Wukong", "", "Sha Monk "};
Private AutoCompleteTextView autoCompleteTextView;
Private MultiAutoCompleteTextView multiAutoCompleteTextView;
Private ArrayAdapter adapter;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Find the TextView control in main. xml by ID
Text = (TextView) findViewById (R. id. text );
// Find the AutoCompleteTextView control in main. xml by ID
AutoCompleteTextView = (AutoCompleteTextView) findViewById (R. id. autoCompleteTextView );
// Set an Array adapter to put Array data into the adapter
Adapter = new ArrayAdapter (this,
Android. R. layout. simple_dropdown_item_1line, item );
// Adapt AutoCompleteTextView
AutoCompleteTextView. setAdapter (adapter );
// Set the listener for AutoCompleteTextView
AutoCompleteTextView. setOnItemClickListener (new OnItemClickListener ()
{
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2,
Long arg3)
{
String str = "this time the goblin" + autoCompleteTextView. getText (). toString ()
+ "Captured! ";
UpdateText (str );
}
});
// Find the MultiAutoCompleteTextView control in main. xml by ID
MultiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById (R. id. multiAutoCompleteTextView );
// Adapt MultiAutoCompleteTextView
MultiAutoCompleteTextView. setAdapter (adapter );
// Set the delimiter. The default Delimiter is comma (,)
MultiAutoCompleteTextView. setTokenizer (new MultiAutoCompleteTextView. CommaTokenizer ());
// Set the MultiAutoCompleteTextView listener
MultiAutoCompleteTextView. setOnItemClickListener (new OnItemClickListener ()
{
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2,
Long arg3)
{
String str = "this time the goblin" + multiAutoCompleteTextView. getText (). toString ()
+ "Captured! ";
UpdateText (str );
}
});
}
Private void updateText (String string)
{
// Set the text information to display in the TextView control.
Text. setText (string );
}
}
Well, you have written all the code. You can select one or more of them, as shown in the figure below:
Many people know about the AutoCompleteTextView control, but what is the role of MultiAutoCompleteTextView?
In fact, you will use the MultiAutoCompleteTextView control when sending a text message to a group. Otherwise, you will lose your text message every time. It is too troublesome. You only need to add a number after the recipient to use MultiAutoCompleteTextView.
From: kangkangz4 Column