yhw-miracle

Android动态自动匹配输入框

痛点就是起点 writed in

本文为痛点就是起点原创文章,可以随意转载,但需注明出处。

动态自动匹配输入框,可以分为单匹配和多匹配两种,如百度搜索框和邮箱发件人的输入框。

  • 单匹配(AutoCompleteTextView)
    • 初始化 AutoCompleteTextView 控件
    • 需要一个适配器
    • 初始化数据源
    • 将 adapter 与 acTextView 绑定
AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.AutoCompleteTextView1);
String[] data1 = {"Android","Book","system","haha","hehe","shabi"};
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,data1);
acTextView.setAdapter(adapter1);
  • 多匹配(MultiAutoCompleteTextView)
    • 初始化 MultiAutoCompleteTextView 控件
    • 需要一个适配器
    • 初始化数据源
    • 将 adapter 与 acTextView 绑定
    • 设置分隔符
MultiAutoCompleteTextView macTextView = (MultiAutoCompleteTextView)findViewById(R.id.MultiAutoCompleteTextView1);
String[] data2 = {"123","12345","haharr","httt,"rr"};
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,data2);
macTextView.setAdapter(adapter2);
macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

该方法只支持逗号连接,若需要自定义分隔符,需要自定义类,然后用语句

macTextView.setTokenizer(new SemicolonTokenizer(';'));

执行。自定义分隔符类的代码如下。

import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.MultiAutoCompleteTextView;

/**
 * Created by 痛点就是起点 on 2017/9/13.
 * @effect 自定义 MultiAutoCompleteTextView 添加分隔符
 */

public class SemicolonTokenizer implements MultiAutoCompleteTextView.Tokenizer {

    private char charS;
    private String mSTring;

    public SemicolonTokenizer(char charS) {
        this.charS = charS;
        mSTring = String.valueOf(charS);
    }

    @Override
    public int findTokenStart(CharSequence text, int cursor) {
        int i = cursor;

        while (i > 0 && text.charAt(i - 1) != charS) {
            i--;
        }
        while (i < cursor && text.charAt(i) == ' ') {
            i++;
        }

        return i;
    }

    @Override
    public int findTokenEnd(CharSequence text, int cursor) {
        int i = cursor;
        int len = text.length();

        while (i < len) {
            if (text.charAt(i) == charS) {
                return i;
            } else {
                i++;
            }
        }

        return len;
    }

    @Override
    public CharSequence terminateToken(CharSequence text) {
        int i = text.length();

        while (i > 0 && text.charAt(i - 1) == ' ') {
            i--;
        }

        if (i > 0 && text.charAt(i - 1) == charS) {
            return text;
        } else {
            if (text instanceof Spanned) {
                SpannableString sp = new SpannableString(text + mSTring);
                TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);
                return sp;
            } else {
                return text + mSTring;
            }
        }
    }
}
Android 自动匹配
技术案例

欢迎关注,我们一起进行认知迭代!


痛点就是起点

© 2016 - 2020 基于 jekyll | Github Pags | iconfont By yhw-miracle