Hướng dẫn sử dụng Android AutoCompleteTextView & MultiAutoCompleteTextView
Công ty Vĩnh Cửu tuyển dụng lập trình viên Java

1- AutoCompleteTextView và MultiAutoCompleteTextView

  • TODO: Chém gió thêm.

2- Ví dụ với AutoCompleteTextView và MultiAutoCompleteTextView

Tạo một project có tên AndroidAutoCompleteTextView:
Giao diện ứng dụng:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Full name"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="20dp">
        <requestFocus />
    </EditText>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Country"
        android:id="@+id/textView2"
        android:layout_below="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/editText"
        android:layout_alignEnd="@+id/editText"
        android:layout_marginTop="20dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Programming languges"
        android:id="@+id/textView3"
        android:layout_below="@+id/autoCompleteTextView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="20dp" />

    <MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/autoCompleteTextView"
        android:layout_alignEnd="@+id/autoCompleteTextView"
        android:layout_marginTop="20dp" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:id="@+id/button"
        android:layout_below="@+id/multiAutoCompleteTextView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="31dp" />

</RelativeLayout>
MainActivity.java
package org.o7planning.androidautocompletetextview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;


public class MainActivity extends Activity {

  private EditText textFullname;
  private  AutoCompleteTextView textCountry;
  private  MultiAutoCompleteTextView textProgrammingLanguage;

  private Button buttonSubmit;

  private String[] countries = {"Vietnam","England","Canada", "France","Australia"};

  private String[] languages={"Java ","CSharp","Visual Basic","Swift","C/C++"};

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      textFullname = (EditText) findViewById(R.id.editText);
      textCountry =(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
      textProgrammingLanguage =(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView);
      buttonSubmit= (Button) findViewById(R.id.button);

      ArrayAdapter adapterCountries = new ArrayAdapter(this,android.R.layout.simple_list_item_1,countries);

      textCountry.setAdapter(adapterCountries);


      // Sét đặt số ký tự nhỏ nhất, để cửa sổ gợi ý hiển thị
      textCountry.setThreshold(1);

      ArrayAdapter adapterLanguages = new ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);

      textProgrammingLanguage.setAdapter(adapterLanguages);

      textProgrammingLanguage.setThreshold(1);

 
      // Các đoạn text ngăn cách nhau bởi dấu phẩy.
      textProgrammingLanguage.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

      buttonSubmit.setOnClickListener(new Button.OnClickListener(){

          @Override
          public void onClick(View v) {
              submitForm();
          }
      });
  }

  private void submitForm() {
      String text="Fullname: "+ this.textFullname.getText().toString()
              +"\nCountry: "+ this.textCountry.getText().toString()
              +"\nLanguages: "+ this.textProgrammingLanguage.getText().toString();

      Toast.makeText(this, text,Toast.LENGTH_LONG).show();

  }

}
Chạy ứng dụng: