Android的高级控件

高级控件-自动文本填充

自动文本填充的思路:

1. 获取数据源
2. 创建适配器
3. 绑定适配器

线性布局下创建线性布局

 

定义全局变量

 //定义数据源
    private String[] data1 ;
    //定义数组适配器
    private ArrayAdapter arrayAdapter1;
    //定义自动文本框(单一文本)
    private AutoCompleteTextView autoCompleteTextView;

    //定义多文本填充
    private MultiAutoCompleteTextView multiAutoCompleteTextView;

    private ArrayAdapter arrayAdapter2;

    //下拉框
    private Spinner spinner;

获取自动文本填充的id

        autoCompleteTextView = findViewById(R.id.actv_main_seacher);
        multiAutoCompleteTextView = findViewById(R.id.actv_main_seacher_1);
        //公用的方法(适配器的绑定)
        initView();

创建并且绑定适配器

 private void initView() {
       //绑定自动的适配器
        arrayAdapter1 = new ArrayAdapter(this,R.layout.actv_main_item1,new String[]{"footBall","basketBall","sconds","love","xiix"});
       autoCompleteTextView.setAdapter(arrayAdapter1);
        //绑定适配器
        arrayAdapter2 = new ArrayAdapter(this,R.layout.actv_main_item1,new String[]{"footBall","basketBall","sconds","love","xiix"});
        multiAutoCompleteTextView.setAdapter(arrayAdapter2);


    }

连接夜神模拟器测试
效果图如下:
Android的高级控件_第1张图片
注意:如果你需要多文本填充那就需要下面两行代码

   //设置分隔符号(默认逗号)
        MultiAutoCompleteTextView.Tokenizer tokenizer = new MultiAutoCompleteTextView.CommaTokenizer();
        multiAutoCompleteTextView.setTokenizer(tokenizer);

你可能感兴趣的:(学习笔记)