如何在自动完成文本视图中设置适配器?
在进入示例之前,我们应该了解 Android 中的自动完成文本视图是什么。自动完成文本视图就像一个编辑文本,它是 editext 的子类,但它将从列表中显示建议作为下拉列表。我们必须为自动完成文本视图设置阈值。例如,我们将其阈值设置为 1,因此如果用户输入一个字母,它将根据阈值字母给出建议。
此示例演示了如何为自动完成文本视图设置适配器。
步骤 1 - 在 Android Studio 中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建新项目。
步骤 2 - 将以下代码添加到 res/layout/activity_main.xml 中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="fill_parent"
android:hint="Enter programming language"
android:layout_height="wrap_content" />
</LinearLayout>在上面,我们声明了自动完成文本视图,当用户输入字母时,它将在下拉菜单中显示列表作为建议。
步骤 3 - 将以下代码添加到 src/MainActivity.java 中
package com.example.andy.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RadioButton radioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AutoCompleteTextView autoCompleteTextView=findViewById(R.id.autoComplete);
ArrayList arrayList=new ArrayList<>();
arrayList.add("Android");
arrayList.add("JAVA");
arrayList.add("CPP");
arrayList.add("C Programming");
arrayList.add("Kotlin");
arrayList.add("CSS");
arrayList.add("HTML");
arrayList.add("PHP");
arrayList.add("Swift");
ArrayAdapter arrayAdapter=new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, arrayList);
autoCompleteTextView.setAdapter(arrayAdapter);
autoCompleteTextView.setThreshold(1);
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d("beforeTextChanged", String.valueOf(s));
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("onTextChanged", String.valueOf(s));
}
@Override
public void afterTextChanged(Editable s) {
Log.d("afterTextChanged", String.valueOf(s));
}
});
}
}在上面的代码中,我们已将一些值存储在 ArrayList 中并将 ArrayList 附加到数组适配器。我们已将适配器设置为自动完成文本视图并将阈值添加为 1。让我们尝试运行您的应用程序。我假设您已将您的实际 Android 移动设备连接到您的计算机。要从 Android Studio 运行应用程序,请打开您项目中的一个活动文件,然后单击工具栏中的运行
图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 -

最初,它将显示如上所示的屏幕,并在该文本视图中输入 ja,它将显示适配器中的结果,如下所示 -

在上述结果中,我们只有一个建议,删除 J 并在该文本视图中键入 c,它将显示多个建议,如下所示 -

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP