zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android AutoCompleteTextView自动提示文本框

Android自动 提示 文本框
2023-09-11 14:14:54 时间



效果  




使用方法: 


在布局文件中:


    <AutoCompleteTextView
        android:id="@+id/autotextview"
        android:completionThreshold="2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

这里的一个属性 completionThreshold 设置的为2,就是说在文本框中输入两个相关的字符后才可弹出相关提示,当然其默认的就是输入两个字符后提出提示


在java中 


 private AutoCompleteTextView mAutotextview;

    private void assignViews() {
        mAutotextview = (AutoCompleteTextView) findViewById(R.id.autotextview);
    }
    private void setAutoCompleteTextView() {
        //创建数据源
        String[] strings = {"adf0","adf1","adf2","adf3","adf4","adf5"};
        //创建数据适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout
                .simple_expandable_list_item_1,strings);
        //设置适配器的样式
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mAutotextview.setAdapter(adapter);
    }


这里设置的数据源是 adf相关的,所以在输入框中输入的文本与adf相关的时候才会显示出相关的提示