1. 程式人生 > >Android學習之AutoCompleteTextView和MultiAutoCompleteTextView

Android學習之AutoCompleteTextView和MultiAutoCompleteTextView

instance java private nis imp vertica @override array .com

轉自:http://blog.csdn.net/qq_28468727/article/details/52258409

AutoCompleteTextView、MultiAutoCompleteTextView自動完成輸入內容的控件

如果我們想實現用戶輸入的內容有自動提示功能,類似百度搜索結果,AutoCompleteTextView和MultiAutoCompleteTextView可以實現類似功能

MainActivity.JAVA:

public class MainActivity extends AppCompatActivity {  
  
    private AutoCompleteTextView autoTextView;  
    
private MultiAutoCompleteTextView multiAutoCompleteTextView; private ArrayList<String> list; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); autoTextView
= (AutoCompleteTextView) findViewById(R.id.autotext); multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiauto); //對list進行初始化 list = new ArrayList<String>(); list.add("123456"); list.add("12345"); list.add("1234"); list.add(
"google"); list.add("goole map"); list.add("goole chrome"); //使用list中的數據對適配器adapter進行初始化 //第二個參數表示適配器為下拉風格 adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,list); //為AutoCompleteTextView添加適配器 autoTextView.setAdapter(adapter); //multiAutoCompleteTextView添加適配器 multiAutoCompleteTextView.setAdapter(adapter); //完成對選項的拆分,以逗號隔開 multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } }

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout 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:orientation="vertical"  
    tools:context="com.example.administrator.edittextdemo.MainActivity">  
  
    <TextView  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:id="@+id/textview1"  
    android:text="AutoCompleteTextView"/>  
  
    <AutoCompleteTextView  
        android:id="@+id/autotext"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" />  
  
    <TextView  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:id="@+id/textview2"  
        android:text="MultiAutoCompleteTextView"/>  
  
    <MultiAutoCompleteTextView  
        android:id="@+id/multiauto"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" />  
</LinearLayout>  

Android學習之AutoCompleteTextView和MultiAutoCompleteTextView