English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes a simple application of Android's AutoCompleteTextView for仿Baidu and Google search auto-suggestion. Share it with everyone for reference, as follows:
Nowadays, when we go online, we almost always use Baidu or Google to search for information. When we enter one or two characters in the input box, it will automatically prompt us with the information we want. How is this effect implemented in Android?63; In fact, with the ArrayAdapter, Android's AutoCompleteTextView Widget can be designed to achieve effects similar to Google search suggestions.
This example first lays out an AutoCompleteTextView Widget in the Layout, then puts the pre-set string array into an ArrayAdapter, and finally uses the AutoCompleteTextView.setAdapter method to enable the AutoCompleteTextView to have an auto-suggestion feature. For example, as soon as 'ab' is entered, a list of all strings containing 'ab' will automatically appear.
Let's take a look at the effect diagram:
Below is the code involved in our program (the code in this example is relatively few):
The first is main.xml:
<?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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Please input:" /> <AutoCompleteTextView android:id="@"+id/actv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
The main control program is AutoCompleteTextViewDemo.Java:
package com.android.test; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class AutoCompleteTextViewDemo extends Activity { private AutoCompleteTextView actv; private static final String[] autoStrs = new String[]{"a","abc","abcd","abcde","ba"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Get actv by findViewById() method actv = (AutoCompleteTextView)findViewById(R.id.actv); //Create a new ArrayAdapter object and pass the autoStr string array into actv ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,autoStrs); actv.setAdapter(adapter); } }
All programs are just a little bit, the great work is done, and finally execute it, will achieve the above effect.
Readers who are interested in more content related to Android can check the special topics on this site: 'Summary of Android View View Techniques', 'Summary of Android Layout Layout Techniques', 'Summary of Android Graphics and Image Processing Techniques', 'Android Development Tutorial for Beginners and Advanced', 'Summary of Android Debugging Techniques and Common Problem Solutions', 'Summary of Android Multimedia Operation Techniques (audio, video, recording, etc.)', 'Summary of Android Basic Component Usage', and 'Summary of Android Widget Usage'
I hope the content described in this article will be helpful to everyone in Android program design.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @ to report, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.)