Android输入框自动提示

Android用的有两种方法AutoCompleteTextViewMultiAutoCompleteTextView,第二种可以连续提示输入,如下图

 Android输入框自动提示_第1张图片Android输入框自动提示_第2张图片


AutoCompleteTextView常用属性  
android:completionHint  设置出现在下拉菜单中的提示标题
android:completionThreshold 设置用户至少输入多少个字符才会显示提示
android:dropDownHorizontalOffset 下拉菜单于文本框之间的水平偏移。默认与文本框左对齐
android:dropDownHeight 下拉菜单的高度
android:dropDownWidth     下拉菜单的宽度
android:singleLine 单行 显示
android:dropDownVerticalOffset   垂直偏移量

布局文件:




    

    

MainActivity里面的代码:

package com.example.administrator.autocompletetextviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

    private AutoCompleteTextView mAutoCompleteTextView;
    private MultiAutoCompleteTextView mMultiAutoCompleteTextView;
    private String[] ceshi1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAutoCompleteTextView=findViewById(R.id.autocomplete);
        mMultiAutoCompleteTextView=findViewById(R.id.multiautocomplete);
        ceshi1 = new String[]{"北京西站", "北京北站", "北京站", "北京南站", "北京大使馆",
                "北京故宫", "天津电视塔", "天津南站", "天津东站", "天津之眼", "天津劝业场","aaa","bbb",
                "aaa北京","北京aaa","aaaa8989","bbb768","北京ab","sss","aaaaa"};
        ArrayAdapter adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_activated_1,ceshi1);
        mAutoCompleteTextView.setAdapter(adapter);
        mMultiAutoCompleteTextView.setAdapter(adapter);
        mMultiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }
}

下载地址:https://download.csdn.net/download/lanrenxiaowen/10746629

你可能感兴趣的:(Android开发)