2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)
Category Android Basic Tutorial
Introduction to This Section:
>
This section continues to explore the widgets of the Adapter class, this time introducing the AutoCompleteTextView (auto-complete text box). I believe you have noticed that widgets related to Adapter can all define their own item styles, right? Or the layout of each item~ play as you wish~ Well, without further ado, let's start the content of this section~ By the way, here is the official API: AutoCompleteTextView
1. Related Attributes:
>
android:completionHint: Sets the hint title in the drop-down menu
android:completionHintView: Defines the hint view to display the drop-down menu
android:completionThreshold: Specifies the minimum number of characters a user must type before hints are displayed
android:dropDownAnchor: Sets the "anchor" component for the drop-down menu. If this attribute is not specified, the TextView will be used as the "anchor" component
android:dropDownHeight: Sets the height of the drop-down menu
android:dropDownWidth: Sets the width of the drop-down menu
android:dropDownHorizontalOffset: Specifies the horizontal spacing between the drop-down menu and the text
android:dropDownVerticalOffset: Specifies the vertical spacing between the drop-down menu and the text
android:dropDownSelector: Sets the click effect of the drop-down menu
android:popupBackground: Sets the background of the drop-down menu
In addition, there is actually a MultiAutoCompleteTextView (auto-complete text box with multiple hints) that has similar functions and attributes to this AutoCompleteTextView. What is the specific difference between them? We will experience it in the following code~ Also, both of these are full-word matching, for example, "little pig": if you type "small" -> it will suggest "little pig", but if you type "pig pig" -> it will not suggest "little pig"!
2. Code Example:
Running Effect Picture :
Implementation Code :
Here we will not customize the layout, but directly use ArrayAdapter to implement it!
Layout file: activity_main.xml :
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@+id/atv_content"
android:layout_width="match_parent"
android:layout_height="48dp"
android:completionHint="Please enter search content"
android:completionThreshold="1"
android:dropDownHorizontalOffset="5dp" />
<MultiAutoCompleteTextView
android:id="@+id/matv_content"
android:layout_width="match_parent"
android:layout_height="48dp"
android:completionThreshold="1"
android:dropDownHorizontalOffset="5dp"
android:text="" />
</LinearLayout>
MainActivity.java :
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView atv_content;
private MultiAutoCompleteTextView matv_content;
private static final String[] data = new String[]{
"Little Pig", "Little Dog", "Little Chicken", "Little Cat", "Little Meow Meow"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
atv_content = (AutoCompleteTextView) findViewById(R.id.atv_content);
matv_content = (MultiAutoCompleteTextView) findViewById(R.id.matv_content);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.
this, android.R.layout.simple_dropdown_item_1line, data);
atv_content.setAdapter(adapter);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, data);
matv_content.setAdapter(adapter2);
matv_content.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
Partial Code Analysis :
>
android:completionThreshold="1": Here we set it to show hints after typing one character
android:completionHint="Please enter search content": This is the text displayed at the bottom of the box. If you think it's ugly, you can set an android:completionHintView with
2.5.4 AutoCompleteTextView (Autocomplete Text Box) Basic Usage
2.5.8 Notification (Status Bar Notification) Detailed Explanation
3.6 Responding to System Setting Events (Configuration Class)
[4.3.2 In-Depth Analysis of BroadcastReceiver](android-tutorial-broadcastreceiver
8.3.4 Paint API - Xfermode and PorterDuff Detailed Explanation (Part One)
8.3.5 Paint API - Xfermode and PorterDuff Detailed Explanation (Part Two)
8.3.6 Paint API - Xfermode and PorterDuff Detailed Explanation (Part Three)
8.3.7 Paint API - Xfermode and PorterDuff Detailed Explanation (Part Four)
8.3.8 Paint API - Xfermode and PorterDuff Detailed Explanation (Part Five)
8.3.14 Several Enumeration/Constant Values of Paint and ShadowLayer Shadow Effect
8.3.17 Detailed Explanation of Canvas API (Part 2) - Collection of Clipping Methods
8.3.18 Detailed Explanation of Canvas API (Part 3) - Matrix and drawBitmapMesh
8.4.3 Android Animation Collection - Property Animation - First Encounter
8.4.4 Android Animation Collection - Property Animation - Re-encounter
[10.5 AlarmManager (Alarm