Android实现登录邮箱的自动补全功能

先看下效果图:

Android实现登录邮箱的自动补全功能_第1张图片

只要输入到@符,就会开始联想邮箱,样式可以自己定义。

下面看下主要的代码:

//这个就是我们继承自MultiAutoCompleteTextView实现我们自定义的邮箱联想组件

public class MailBoxAssociateView extends android.support.v7.widget.AppCompatMultiAutoCompleteTextView {
    public MailBoxAssociateView(Context context) {
        super(context);
    }

    public MailBoxAssociateView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MailBoxAssociateView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean enoughToFilter() {
        // 如果字符中包含'@'并且不在第一位,则满足条件
        return getText().toString().contains("@") && getText().toString().indexOf("@") > 0;
    }
}

指定从哪个地方开始联想

public class MailBoxAssociateTokenizer implements MultiAutoCompleteTextView.Tokenizer {

    @Override
    public int findTokenEnd(CharSequence text, int cursor) {
        int i = cursor;
        int len = text.length();
        while (i < len) {
            if (text.charAt(i) == '@') {
                return i;
            } else {
                i++;
            }
        }
        return len;
    }

    @Override
    public int findTokenStart(CharSequence text, int cursor) {
        int index = text.toString().indexOf("@");

        if (index < 0) {
            index = text.length();
        }
        if (index >= findTokenEnd(text, cursor)) {
            index = 0;
        }
        return index;
    }

    @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) == '@') {
            return text;
        } else {
            if (text instanceof Spanned) {
                SpannableString sp = new SpannableString(text);
                TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);
                return sp;
            } else {
                return text;
            }
        }
    }

}
布局文件,这里面的颜色都可以根据自己的需要自行调整


主代码

public class MainActivity extends AppCompatActivity {

    MailBoxAssociateView view;
    private String[] email_sufixs = new String[]{"@qq.com", "@163.com", "@126.com", "@gmail.com", "@sina.com", "@hotmail.com",
            "@yahoo.cn", "@sohu.com", "@foxmail.com", "@139.com", "@yeah.net", "@vip.qq.com", "@vip.sina.com"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view = findViewById(R.id.imageviewedittextone);
        MailBoxAssociateTokenizer mailBoxAssociateTokenizer = new MailBoxAssociateTokenizer();
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_dropdown_item_1line, email_sufixs);
        view.setAdapter(adapter);
        view.setTokenizer(mailBoxAssociateTokenizer);


    }
}





你可能感兴趣的:(Android代码)