Trước khi đi vào ví dụ, chúng ta nên biết AutoCompleteTextView là gì trong Android. AutoCompleteTextView giống như EditText và là subclass của EditText, nhưng nó hiển thị gợi ý từ một danh sách dưới dạng dropdown. Chúng ta phải thiết lập giá trị Threshold cho AutoCompleteTextView. Ví dụ, nếu thiết lập Threshold là 1, khi người dùng nhập 1 ký tự nó sẽ gợi ý theo ký tự đó.
Ví dụ này minh họa cách thiết lập adapter cho AutoCompleteTextView.
Bước 1
Tạo một dự án mới trong Android Studio, vào File → New Project và điền đầy đủ thông tin cần thiết để tạo dự án mới.
Bước 2
Thêm mã sau vào res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://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>
Ở trên, chúng ta khai báo AutoCompleteTextView, khi người dùng nhập một ký tự, nó sẽ hiển thị danh sách gợi ý trong menu dropdown.
Bước 3
Thêm mã sau vào 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)); } }); } }Trong mã trên, chúng ta lưu một số giá trị trong ArrayList và gán ArrayList vào ArrayAdapter. Chúng ta đặt adapter cho AutoCompleteTextView và thêm threshold là 1. Hãy thử chạy ứng dụng. Giả sử bạn đã kết nối thiết bị Android thực tế với máy tính. Để chạy ứng dụng từ Android Studio, mở một file activity của dự án và nhấn biểu tượng Run từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn và kiểm tra thiết bị sẽ hiển thị màn hình mặc định:

Ban đầu, nó hiển thị màn hình như trên. Khi nhập "ja" vào TextView, nó sẽ hiển thị kết quả từ adapter như hình dưới:

Trong kết quả trên, chúng ta chỉ có một gợi ý. Xóa "J" và nhập "c" vào TextView, nó sẽ hiển thị nhiều gợi ý như hình dưới:
