Android: AutoCompleteTextView trong Android
Một AutoCompleteTextView là môt view tương tự như EditText, ngoại trừ rằng nó hiển thị một danh sách các gợi ý tự động khi người dùng nhập văn bản.
Danh sách các gợi ý này được hiển thị trong dạng drop-down menu. Người dùng có thể chọn một item từ đó để thay thế với nội dung của edit box.
Thuộc tính của AutoCompleteTextView trong Android
Bảng dưới liệt kê một số thuộc tính quan trọng liên quan tới AutoCompleteTextView Control. Bạn có thể kiểm tra Android Offical Documentation để có danh sách đầy đủ các thuộc tính và các phương thức để thay đổi các thuộc tính này tại runtime.
Thuộc tính | Miêu tả |
---|---|
android:completionHint | Định nghĩa một text gợi ý được hiển thị trong drop-down menu |
android:completionHintView | Định nghĩa một view gợi ý được hiển thị trong drop-down menu |
android:completionThreshold | Định nghĩa số ký tự mà người dùng phải soạn trước khi các gợi ý đầy đủ được hiển thị trong một drop-down menu |
android:dropDownAnchor | Đây là View để neo một drop-down |
android:dropDownHeight | Xác định chiều cao cơ bản của dropdown |
android:dropDownHorizontalOffset | Số pixel từ đó drop-down nên là offset theo chiều ngang |
android:dropDownSelector | Đây là bộ chọn trong một danh sách dropdown |
android:dropDownVerticalOffset | Số pixel từ đó drop-down nên là offset theo chiều dọc |
android:dropDownWidth | Xác định độ rộng cơ bản của dropdown |
android:popupBackground | Thiết lập background |
Ví dụ
Ví dụ sau sẽ minh họa cách ta tạo ứng dụng Android với việc sử dụng LinearLayout và AutoCompleteTextView.
Sau đây là nội dung của file MainActivity.java:
package v1study.com.autocompletetextviewv1study; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class MainActivity extends AppCompatActivity { AutoCompleteTextView autoCompleteTextView; String arr[] = {"Paris, France", "PA, United State", "Parana, Brazil", "Padua, Itali", "Pasadena, CA, United State"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); autoCompleteTextView = findViewById(R.id.autoCompleteTextView); ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, R.layout.select_dialog_item, arr); autoCompleteTextView.setThreshold(2); autoCompleteTextView.setAdapter(adapter); } }
Sau đây là nội dung của file res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/autocompletetextview" android:textSize="30sp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintVertical_bias="0.175" android:textStyle="bold" android:textColor="#4CAF50"/> <AutoCompleteTextView android:id="@+id/autoCompleteTextView" android:layout_width="357dp" android:layout_height="53dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintVertical_bias="0.356" android:textColor="#E91E63"/> </androidx.constraintlayout.widget.ConstraintLayout>
Sau đây là nội dung của res/values/strings.xml:
<resources> <string name="app_name">AutoCompleteTextViewV1Study</string> <string name="autocompletetextviewv1study">AutoCompleteTextViewV1Study</string> <string name="autocompletetextview">AutoCompleteTextView</string> </resources>
Chạy ứng dụng, màn hình sau sẽ xuất hiện sau khi bạn soạn "pa":