AutocompleteTextView is an editable text view that shows completion suggestions automatically while the user is typing in android apps. In this tutorial we’ll implement android AutoCompleteTextView in our application using an ArrayAdapter to define the list of suggestions.

AutocompleteTextView是可编辑的文本视图,当用户在android应用中键入内容时,它会自动显示完成建议。 在本教程中,我们将使用ArrayAdapter在我们的应用程序中实现android AutoCompleteTextView来定义建议列表。

Android AutoCompleteTextView概述 (Android AutoCompleteTextView Overview)

AutoCompleteTextView is a component used to show suggestions while writing in an editable text field. The suggestions list is shown in a drop down menu from which a user can select the desired item. The list of suggestions is obtained from an adapter and it appears only after a number of characters that are specified in the threshold.

AutoCompleteTextView是一个组件,用于在可编辑文本字段中书写时显示建议。 建议列表显示在下拉菜单中,用户可以从中选择所需的项目。 建议列表是从适配器获得的,仅在阈值中指定的多个字符之后出现。

In order to use an AutoCompleteThreshold field, it needs to be defined in the xml layout as follows:

为了使用AutoCompleteThreshold字段,需要在xml布局中定义它,如下所示:

Note : android:ems or setEms(x) sets the width of a TextView to fit a text of x ‘M’ number of letters regardless of the actual text extension and text size.

注意: android:emssetEms(x)设置TextView的宽度以适合x'M '个字母的文本,而不管实际的文本扩展名和文本大小如何。

Some important methods of Autocomplete list are given below:

自动完成列表的一些重要方法如下:

  1. getAdapter() : This method returns a filterable list adapter used for auto completion

    getAdapter() :此方法返回用于自动完成的可过滤列表适配器
  2. getCompletionHint() : This method returns optional hint text displayed at the bottom of the the matching list

    getCompletionHint() :此方法返回显示在匹配列表底部的可选提示文本
  3. getDropDownAnchor() : This method returns returns the id for the view that the auto-complete drop down list is attached to

    getDropDownAnchor() :此方法返回为其附加自动完成下拉列表的视图的ID。
  4. getListSelection() : This method returns the position of the dropdown view selection, if there is any

    getListSelection() :此方法返回下拉视图选择的位置(如果有)
  5. isPopupShowing() : This method indicates whether the popup menu is showing

    isPopupShowing() :此方法指示是否显示弹出菜单
  6. setText(CharSequence text, boolean filter) : This method sets text except that it can disable filtering

    setText(CharSequence text,boolean filter) :此方法设置文本,但可以禁用过滤
  7. showDropDown() : This method displays the drop down on screen

    showDropDown() :此方法在屏幕上显示下拉菜单

The setAdapter method is used to set the adapter of the autoCompleteTextView.

setAdapter方法用于设置autoCompleteTextView的适配器。

Let’s jump to the coding part of it.

让我们跳到它的编码部分。

Android AutoCompleteTextView项目结构 (Android AutoCompleteTextView Project Structure)

This project contains a simple TextView and a AutoCompleteTextView in the layout of the MainActivity. The ArrayAdapter contains the following fruits : Apple, Banana, Cherry, Date, Grape, Kiwi, Mango, Pear.

该项目在MainActivity的布局中包含一个简单的TextView和一个AutoCompleteTextView 。 ArrayAdapter包含以下水果:苹果,香蕉,樱桃,枣,葡萄,猕猴桃,芒果,梨。

Android AutoCompleteTextView示例代码 (Android AutoCompleteTextView Example Code)

The layout of the MainActivity is defined as follows.

MainActivity的布局定义如下。

activity_main.xml

activity_main.xml

                    

The MainActivity.java is defined below.

MainActivity.java在下面定义。

package com.journaldev.autocomplete;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;public class MainActivity extends Activity {    String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Grape", "Kiwi", "Mango", "Pear"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //Creating the instance of ArrayAdapter containing list of fruit names        ArrayAdapter adapter = new ArrayAdapter                (this, android.R.layout.select_dialog_item, fruits);        //Getting the instance of AutoCompleteTextView        AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);        actv.setThreshold(1);//will start working from first character        actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView        actv.setTextColor(Color.RED);    }}

In the above code, we’ve stored the list of fruits in an ArrayAdapter with a layout imported from the Android SDK.

在上面的代码中,我们将水果列表存储在具有从Android SDK导入的布局的ArrayAdapter中。

The text color in the editable TextView is red. A threshold count of 1 depicts that to show the autocomplete drop-down list we need to type in at least one character.

可编辑TextView中的文本颜色为红色。 阈值计数为1表示要显示自动完成下拉列表,我们需要至少键入一个字符。

Note: Autocomplete list is visible only when the Editable field is focused.

注意 :仅当“可编辑”字段为焦点时,“自动完成列表”才可见。

Below is our auto complete example application in execution.

以下是我们正在执行的自动完成示例应用程序。

This brings an end to Android AutoCompleteTextView tutorial. You can download the final Android AutoComplete project from the link given below.

这样就结束了Android AutoCompleteTextView教程。 您可以从下面的链接下载最终的Android AutoComplete项目

Download Android AutoCompleteTextView Project 下载Android AutoCompleteTextView项目

翻译自: https://www.journaldev.com/9574/android-autocompletetextview-example-tutorial

更多相关文章

  1. Android 列表对话框
  2. Android 富文本编辑器 - ListItemSpan
  3. Android SDK Manager 更新失败的解决方法
  4. Android三种方法设置ImageView的图片
  5. android SDK manager 无法获取更新版本列表
  6. 全志A64 Android7.1屏蔽使用按键进入安全模式的方法
  7. Android使用AttributeSet自定义控件的方法

随机推荐

  1. android mediaplayer 音效 代码
  2. [android开发必备]Android开发者社区汇总
  3. Speeding Up the Android Emulator on In
  4. findbugs在android studio中的使用
  5. android UI布局练习3
  6. android 加密数据库
  7. android 编译 英文说明文档
  8. Android中Preference的使用以及监听事件
  9. Android的四种点击事件
  10. Android(安卓)GPS定位 获取经纬度