การใช้งาน AutoCompleteTextView สำหรับคำแนะนำข้อความอัตโนมัติ
ตัวอย่างนี้แสดงวิธีใช้ AutoCompleteTextView (ผ่าน AppCompat) เพื่อให้ผู้ใช้เลือกข้อมูลจากรายการที่กำหนดไว้ขณะพิมพ์
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project → กรอกรายละเอียด → Finish
ขั้นตอนที่ 2: Layout - res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="@+id/autoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ป้อนชื่อผลไม้"
android:textColor="#000000"
android:textColorHint="#000000" />
</LinearLayout>
ขั้นตอนที่ 3: MainActivity.java
package app.tutorialspoint.com.sample;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
import android.widget.ArrayAdapter;
public class MainActivity extends AppCompatActivity {
private String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Grape", "Kiwi", "Mango", "Pear"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatAutoCompleteTextView autoTextView = findViewById(R.id.autoTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this,
android.R.layout.select_dialog_item,
fruits
);
autoTextView.setThreshold(1); // เริ่มค้นหาตั้งแต่ตัวอักษรแรก
autoTextView.setAdapter(adapter);
}
}
ขั้นตอนที่ 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.tutorialspoint.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
คุณสมบัติสำคัญ
| Method/Attribute | คำอธิบาย |
|---|---|
| setThreshold(n) | จำนวนตัวอักษรขั้นต่ำก่อนแสดงคำแนะนำ (default=2) |
| setAdapter(adapter) | กำหนด Adapter ที่มีข้อมูล |
| setDropDownHeight() | ความสูงของ Dropdown |
| setDropDownWidth() | ความกว้างของ Dropdown |
| setDropDownBackgroundResource() | พื้นหลัง Dropdown |
| setOnItemClickListener() | จับ Event เมื่อเลือก Item |
การปรับแต่งเพิ่มเติม
1. Custom Adapter สำหรับ Layout เฉพาะตัว
public class FruitAdapter extends ArrayAdapter<String> {
public FruitAdapter(Context context, String[] fruits) {
super(context, R.layout.dropdown_item, fruits);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Custom layout สำหรับแต่ละ item
}
}
2. รับค่าเมื่อผู้ใช้เลือก
autoTextView.setOnItemClickListener((parent, view, position, id) -> {
String selected = (String) parent.getItemAtPosition(position);
Toast.makeText(this, "เลือก: " + selected, Toast.LENGTH_SHORT).show();
});
3. ใช้กับข้อมูลจาก Database/Server
// ใช้ Filterable interface ใน Adapter เพื่อ Filter แบบ Real-time
// หรือใช้ MaterialAutoCompleteTextView (Material Components)
<com.google.android.material.textfield.MaterialAutoCompleteTextView
...
app:dropDownBackground="@color/white" />
การทดสอบ
รันแอป → พิมพ์ "A" → จะแสดง "Apple" → พิมพ์ "B" → แสดง "Banana" ฯลฯ