1. Introduction
Google or Baidu is often used when we access the internet. If we enter the information we want in the input box, other prompts related to it will appear, which is very convenient. In Android, The AutoCompleteTextView implemented by AutoCompleteTextView is an editable text view. When you type this view, the suggestion information is displayed automatically.
The suggestion list is displayed in the drop-down list box. You can select an item to replace the content in the edit box.
When you click ENTER or click ENTER, the drop-down list automatically disappears.
The recommended list is the data obtained from a data adapter.
[Java]
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. ArrayAdapter;
Import android. widget. AutoCompleteTextView;
Import android. widget. AdapterView. OnItemClickListener;
Public class AutoActivity extends Activity
{
Private AutoCompleteTextView autoCompleteTextView = null;
Private final String [] Citys = new String []
{
"Belgium", "France", "Italy", "Germany", "Spain", "Sppp", "Itaggg"
};
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
AutoCompleteTextView = (AutoCompleteTextView) findViewById (R. id. AutoCompleteTextView );
// Configure and set hosts
ArrayAdapter <String> arrayAdapter = new ArrayAdapter <String> (
AutoActivity. this,
Android. R. layout. simple_dropdown_item_1line, Citys );
AutoCompleteTextView. setAdapter (arrayAdapter );
AutoCompleteTextView. setOnItemClickListener (new OnItemClickListener ()
{
@ Override
Public void onItemClick (AdapterView <?> Parent, View view,
Int position, long id)
{
System. out. println (autoCompleteTextView. getText (). toString ());
}
});
}
}
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. ArrayAdapter;
Import android. widget. AutoCompleteTextView;
Import android. widget. AdapterView. OnItemClickListener;
Public class AutoActivity extends Activity
{
Private AutoCompleteTextView autoCompleteTextView = null;
Private final String [] Citys = new String []
{
"Belgium", "France", "Italy", "Germany", "Spain", "Sppp", "Itaggg"
};
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
AutoCompleteTextView = (AutoCompleteTextView) findViewById (R. id. AutoCompleteTextView );
// Configure and set hosts
ArrayAdapter <String> arrayAdapter = new ArrayAdapter <String> (
AutoActivity. this,
Android. R. layout. simple_dropdown_item_1line, Citys );
AutoCompleteTextView. setAdapter (arrayAdapter );
AutoCompleteTextView. setOnItemClickListener (new OnItemClickListener ()
{
@ Override
Public void onItemClick (AdapterView <?> Parent, View view,
Int position, long id)
{
System. out. println (autoCompleteTextView. getText (). toString ());
}
});
}
}
[Java]
<Span style = "white-space: pre; background-color: rgb (240,240,240);"> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" </span>
<Span style = "white-space: pre; background-color: rgb (240,240,240);"> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" </span> [java]
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"
Android: completionThreshold = "1"
/>
</LinearLayout>
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"
Android: completionThreshold = "1"
/>
</LinearLayout>
Supplement:
CompletionThreshold: The value determines that you enter at least a few characters in AutoCompleteTextView to automatically prompt. In addition, up to 20 messages are displayed by default.
DropDownAnchor: its value is a View ID. If it is specified, AutoCompleteTextView will display an automatic prompt under this View.
DropDownSelector: it should be set the background color of the automatic prompt. It has not been tried and needs further research.
DropDownWidth: Set the width of the automatic prompt list.
Public class MultiAutoCompleteTextViewActivity extends Activity
{
Private MultiAutoCompleteTextView autoCom = null ;;
Private Button btnClear = null;
Private String [] normalString = null;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
NormalString = new String []
{"Android", "android sxp", "sxp", "sxp wx "};
AutoCom = (MultiAutoCompleteTextView) findViewById (R. id. multiAutoCompleteTextView );
BtnClear = (Button) findViewById (R. id. btn );
// Simple_dropdown_item_1line
// Some simple styles seem to be custom
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_dropdown_item_1line, normalString );
AutoCom. setAdapter (adapter );
// SetTokenizer (); // Sets the Tokenizer tag generator that will be used
// Determine the relevant range of the text where the user is typing .//
// CommaTokenizer (): // This simple component can be used in some lists that contain items that are separated by commas (,) and one or more spaces. //
// I don't quite understand. If this method is not added, the text cannot be automatically completed. If it is added, it is nonsense. Of course, the list is separated by commas.
AutoCom. setTokenizer (new MultiAutoCompleteTextView. CommaTokenizer ());
BtnClear. setOnClickListener (new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
AutoCom. setText ("");
}
});
}
}
Public class MultiAutoCompleteTextViewActivity extends Activity
{
Private MultiAutoCompleteTextView autoCom = null ;;
Private Button btnClear = null;
Private String [] normalString = null;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
NormalString = new String []
{"Android", "android sxp", "sxp", "sxp wx "};
AutoCom = (MultiAutoCompleteTextView) findViewById (R. id. multiAutoCompleteTextView );
BtnClear = (Button) findViewById (R. id. btn );
// Simple_dropdown_item_1line
// Some simple styles seem to be custom
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_dropdown_item_1line, normalString );
AutoCom. setAdapter (adapter );
// SetTokenizer (); // Sets the Tokenizer tag generator that will be used
// Determine the relevant range of the text where the user is typing .//
// CommaTokenizer (): // This simple component can be used in some lists that contain items that are separated by commas (,) and one or more spaces. //
// I don't quite understand. If this method is not added, the text cannot be automatically completed. If it is added, it is nonsense. Of course, the list is separated by commas.
AutoCom. setTokenizer (new MultiAutoCompleteTextView. CommaTokenizer ());
BtnClear. setOnClickListener (new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
AutoCom. setText ("");
}
});
}
}
[Java]
<? 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: id = "@ + id/textView"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: text = "enter"
Android: textSize = "20px"> </TextView>
<MultiAutoCompleteTextView
Android: id = "@ + id/multiAutoCompleteTextView"
Android: layout_width = "260px"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: text = ""/>
<Button
Android: id = "@ + id/btn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: text = "clear"> </Button>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
Http://www.bkjia.com/index.php? M = content & c = content & a = edit & catid = 90 & id = 120457 & pc_hash = 2 OWOMG android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: id = "@ + id/textView"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: text = "enter"
Android: textSize = "20px"> </TextView>
<MultiAutoCompleteTextView
Android: id = "@ + id/multiAutoCompleteTextView"
Android: layout_width = "260px"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: text = ""/>
<Button
Android: id = "@ + id/btn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: text = "clear"> </Button>
</LinearLayout>
Differences between AutoCompleteTextView and MultiAutoCompleteTextView:
AutoCompleteTextView only matches once
MultiAutoCompleteTextView can be matched multiple times
From running snails