安卓之autocompletetextview

activity_main:






 
 
     
     
     
     
     
     
     



item.xml:



    
    



package com.example.my_autocompletetextview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        //1.准备数据源
        final String string[] = {"北京","上海","广州","1235","hello"};
        //2.准备适配器
        ArrayAdapter adapter = new ArrayAdapter(
                MainActivity.this,     //
                R.layout.item,                 //显示的布局
                R.id.text,                     //显示的字符串
                string);                       //数据源
        //3.让控件和适配器进行关联
        AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autocomplete);
        autoCompleteTextView.setAdapter(adapter);

        //当点击了某一列响应
        autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this,string[i],Toast.LENGTH_LONG).show();
            }
        });
    }

}

效果:(输出代码中具有的字符串的时候,会自动显示出来!)


安卓之autocompletetextview_第1张图片
image.png

你可能感兴趣的:(安卓之autocompletetextview)