Learn android from scratch <AutoCompleteTextView text box. 19.>, android starts from scratch
The prompt function can help users easily input information, and provides similar functions in Android. However, the implementation of this function depends on android. widget. the AutoCompleteTextView class is complete. The Inheritance structure of this class is as follows: java. lang. object upload android. view. view every android. widget. textView upload android. widget. editText watermark android. widget. autoCompleteTextView
No. |
Method |
Type |
Description |
1 |
Public void clearListSelection () |
Normal |
Clear all drop-down list items |
2 |
Public ListAdapter getAdapter () |
Normal |
Retrieve a dataset |
3 |
Public void setAdapter (T adapter) |
Normal |
Set Dataset |
4 |
Public void setOnClickListener (View. OnClickListener listener) |
Normal |
Set a click event |
5 |
Public void setOnItemClickListener (AdapterView. OnItemClickListener l) |
Normal |
Set the click event on the option |
6 |
Public void setOnItemSelectedListener (AdapterView. OnItemSelectedListener l) |
Normal |
Click Event when option is selected |
The following uses a Demo to explain this component.
Xml file
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <AutoCompleteTextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" > </AutoCompleteTextView></RelativeLayout></span>
JAVA files
<Span style = "font-size: 18px;"> package com. example. autocompleteterxtview; 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. toast; public class MainActivity extends Activity {private AutoC OmpleteTextView autoCompleteTextView = null; // create the AutoCompleteTextView object private static final String data [] = {" ", "fengfei Rainstorm", "fenglai ah"}; // set the dataset @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); super. setContentView (R. layout. activity_main); autoCompleteTextView = (AutoCompleteTextView) this. findViewById (R. id. text); // instantiate the object // create an adapter to store data settings Description: ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_dropdown_item_1line, data); this. autoCompleteTextView. setAdapter (adapter); // Click Event settings. I will explain it later. Here I mainly want to test the method of this component. this. autoCompleteTextView. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "you have selected" + data [position], 2 ). show (); // prompt component, prompt message}) ;}</span>
Button component Effect
This component is easy to set. You can try other methods by yourself.
Next forecast, Toast prompt component
In android: AutoCompleteTextView
Use the following code:
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1, books );
Android development: solution to the problem that AutoCompleteTextView can be completed only after two characters are entered
Android provides a textview with the automatic prompt function. You can write several lines of code according to the built-in documents and apidemo to implement this function. However, the default thresh value is 2. Therefore, you must enter two or more characters by default to enable the automatic prompt function. Otherwise, AutoCompleteTextView will not receive any prompts. Of course, you can use setThresh to set a prompt after entering at least a few characters, or you can set it in xml. AutoCompleteTextView matches all your strings as substrings, while MultiAutoCompleteTextView breaks down the strings you have already entered Based on the tokenizer you provided, the last few characters that meet the conditions are automatically prompted as substrings.
For example, if you set MultiAutoCompleteTextView. setTokenizer (newMultiAutoCompleteTextView. CommaTokenizer ());
When you enter It, It will prompt you Italy, and then you choose this string. In this case, the string in textview is "Italy", followed by ", It". In this case, the string is Italy in textview, It, in this case, MultiAutoCompleteTextView breaks down the "It" according to the configured Word Segmentation Method (CommaTokenizer) and matches It again. The prompt Italy is displayed. this is the origin of Multi. Supports multiple word prompts. Of course, you must set the correct Tokenizer ).