简单实现手机号登录(344格式,正则验证)

效果图
简单实现手机号登录(344格式,正则验证)_第1张图片

login.xml(布局两个控件)

    
    

login.java(代码三个类)

@BindView(R.id.Login_login)
Button Login_login;
@BindView(R.id.Login_Phone)
EditText Login_Phone;
private boolean delete = false;
private static int sLastLength;

    Login_Phone.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            return false;
        }
    });
    //输入手机号
    Login_Phone.addTextChangedListener(new TextWatcher() {

        private int oldLength = 0;
        private boolean isChange = true;
        private int curLength = 0;
        private int emptyNumB = 0;  //初始空格数
        private int emptyNumA = 0;  //遍历添加空格后的空格数

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            oldLength = s.length();
            //Log.i(TAG, "未改变长度: " + oldLength);
            emptyNumB = 0;
            for (int i = 0; i < s.toString().length(); i++) {
                if (s.charAt(i) == ' ') emptyNumB++;
            }
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            curLength = s.length();
            //Log.i(TAG, "当前长度: " + curLength);
            //优化处理,如果长度未改变或则改变后长度小于3就不需要添加空格
            if (curLength == oldLength || curLength <= 3) {
                isChange = false;
            } else {
                isChange = true;
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (isChange) {
                if (curLength - sLastLength < 0) {  //判断是editext中的字符串是在减少 还是在增加
                    delete = true;
                } else {
                    delete = false;
                }
                sLastLength = curLength;
                int selectIndex = Login_Phone.getSelectionEnd();//获取光标位置
                String content = s.toString().replaceAll(" ", "");
                //Log.i(TAG, "content:" + content);
                StringBuffer sb = new StringBuffer(content);
                //遍历加空格
                int index = 1;
                emptyNumA = 0;
                for (int i = 0; i < content.length(); i++) {
                    if (i == 2) {
                        sb.insert(i + index, " ");
                        index++;
                        emptyNumA++;
                    } else if (i == 6) {
                        sb.insert(i + index, " ");
                        index++;
                        emptyNumA++;
                    }
                }
                //Log.i(TAG, "result content:" + sb.toString());
                String result = sb.toString();
                //遍历加空格后 如果发现最后一位是空格 就把这个空格去掉
                if (result.endsWith(" ")) {
                    result = result.substring(0, result.length() - 1);
                    emptyNumA--;
                }
                //用遍历添加空格后的字符串 来替换editext变化后的字符串
                s.replace(0, s.length(), result);
                //处理光标位置
                if (emptyNumA > emptyNumB) {
                    selectIndex = selectIndex + (emptyNumA - emptyNumB);
                }
                if (selectIndex > s.length()) {
                    selectIndex = s.length();
                } else if (selectIndex < 0) {
                    selectIndex = 0;
                }
                // 例如"123 45"且光标在4后面 这时需要删除4 光标的处理
                if (selectIndex > 1 && s.charAt(selectIndex - 1) == ' ') {
                    if (delete) {
                        selectIndex--;
                    } else {
                        selectIndex++;
                    }
                }
                Login_Phone.setSelection(selectIndex);
                isChange = false;
            }
        }
    });
    //登录
    Login_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //获取手机号
            String phone = Login_Phone.getText().toString();
            //非空验证
            if (phone.isEmpty()){
                Toast.makeText(LoginActivity.this,"手机号不可为空!!!",Toast.LENGTH_SHORT).show();
            }else {
                //截取手机号,去掉中间的空格
                String pe = "";
                String[] strarray=phone.split("[ ]");
                for (int i = 0; i < strarray.length; i++){
                    pe = pe+strarray[i];
                }
                //正则表达式
                String mobiles = "^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}$";
                //正则验证
                Pattern p = Pattern.compile(mobiles);
                Matcher m = p.matcher(pe);
                boolean flag =  m.matches();
                if (!flag){
                    Toast.makeText(LoginActivity.this,"手机号输入错误!!!",Toast.LENGTH_SHORT).show();
                }else {
                    startActivity(new Intent(LoginActivity.this,ScanCodeActivity.class));
                }
            }
        }
    });

你可能感兴趣的:(个人笔记)