Android control: Exploring AutoCompleteTextView and MultiAutoCompleteTextView

Source: Internet
Author: User

Android provides two types of smart input boxes: AutoCompleteTextView and MultiAutoCompleteTextView. Their functions are roughly the same. The display effect is the same as that in Google search. When you enter some characters in the search box (at least two characters), a drop-down box will pop up prompting similar results. The following describes in detail.

I. AutoCompleteTextView

1. Introduction

An inherited fromEditViewOfThe editable text view can dynamically match the input content. For example, when google search engine inputs text, it can display matching hot information based on the content.

2. Important Methods

ClearListSelection (): clears the selected list items.

DismissDropDown (): If the drop-down menu is closed

GetAdapter (): Get the adapter

 

Ii. MultiAutoCompleteTextView

1. Introduction

An inherited fromAutoCompleteTextViewOfThe editable text view effectively expands the text you type without entering the entire content. (If you enter a part of the content, the system will prompt the remaining part of the content ).

The user must provideMultiAutoCompleteTextView.TokenizerUsed to distinguish different substrings.

2. Important Methods

EnoughToFilter (): filter when the text length exceeds the threshold.

Validate mvalidation (): instead of verifying the entire text, this subclass method verifies each individual text mark

SetTokenizer (MultiAutoCompleteTextView. Tokenizer t): When the user is inputting, The tokenizer setting is used to determine the range of Text

The following are examples

Main. xml layout File

<?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">
<AutoCompleteTextView android:id="@+id/autoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

TextViewActivity class

Package com. ljq. activity;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. ArrayAdapter;
Import android. widget. AutoCompleteTextView;
Import android. widget. MultiAutoCompleteTextView;

Public class TextViewActivity extends Activity {
// Jiudi city, Fujian Province
Private static final String [] cities = new String []
{"FuZhou", "XiaMen", "NiDe", "PuTian", "QuanZhou", "ZhangZhou", "LongYan", "SanMing", "NanPing "};
Private AutoCompleteTextView autoCompleteTextView = null;
Private MultiAutoCompleteTextView multiAutoCompleteTextView = null;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

AutoCompleteTextView = (AutoCompleteTextView) findViewById (R. id. autoCompleteTextView );
MultiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById (R. id. multiAutoCompleteTextView );

// Create an adapter
ArrayAdapter <String> adapter = new ArrayAdapter <String> (
This,
Android. R. layout. simple_dropdown_item_1line,
Cities );
AutoCompleteTextView. setAdapter (adapter );
// Set the number of characters entered and prompt, the default value is 2
AutoCompleteTextView. setThreshold (2 );

MultiAutoCompleteTextView. setAdapter (adapter );
MultiAutoCompleteTextView. setThreshold (2 );
// You must provide a MultiAutoCompleteTextView. Tokenizer to distinguish different substrings.
MultiAutoCompleteTextView. setTokenizer (new MultiAutoCompleteTextView. CommaTokenizer ());
}
}

Running result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.