android AutoCompleteTextView 使用示例

创建布局文件




    
    

    


对应的Activity文件代码如下:

package com.toby.personal.testlistview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

public class MainActivity extends Activity {

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

        String[] array1 = new String[]{"C", "C++", "Cpp", "Qt", "QML", "Java", "PHP", "Android"};

        ArrayAdapter a = new ArrayAdapter<>(this,
                android.R.layout.simple_dropdown_item_1line, array1);

        AutoCompleteTextView autoCompleteText
                = (AutoCompleteTextView) findViewById(R.id.autoCompleteText);

        if (autoCompleteText != null) {
            autoCompleteText.setAdapter(a);
        }

        MultiAutoCompleteTextView multiAutoCompleteText
                = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteText);

        if (multiAutoCompleteText != null) {
            multiAutoCompleteText.setAdapter(a);
            multiAutoCompleteText.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
        }

    }
}

参考文献:《疯狂Android讲义(第2版)》

你可能感兴趣的:(android AutoCompleteTextView 使用示例)