美文网首页
EditText输入电话号码自带空格

EditText输入电话号码自带空格

作者: AiPuff | 来源:发表于2016-12-02 17:25 被阅读36次

    形如:183 2185 9453

    类源码:

     public static int mobileChanged(CharSequence s, int start, int before, int count, EditText editText,int changdu) {
            StringBuilder sb = new StringBuilder();
            int index=s.length();
            if(changdu<s.length()){
                //如果是输入字符
                switch (s.length()){
                    case 4:
                        //1832
                        Log.i("9私聊","4");
                        sb.append(s.charAt(0));
                        sb.append(s.charAt(1));
                        sb.append(s.charAt(2));
                        sb.append(" ");
                        sb.append(s.charAt(3));
    
                        index++;
                        editText.setText(sb.toString());
                        editText.setSelection(index);
                        break;
                    case 9:
                        //183 21859
                        Log.i("9私聊","9");
                        sb.append(s.charAt(0));
                        sb.append(s.charAt(1));
                        sb.append(s.charAt(2));
                        sb.append(s.charAt(3));
                        sb.append(s.charAt(4));
                        sb.append(s.charAt(5));
                        sb.append(s.charAt(6));
                        sb.append(s.charAt(7));
                        sb.append(" ");
                        sb.append(s.charAt(8));
                        index++;
                        editText.setText(sb.toString());
                        editText.setSelection(index);
                        break;
                }
            }else{
                //删除字符
    
                //如果是输入字符
                switch (s.length()){
                    case 4:
                        //183 2
                        Log.i("9私聊","5");
                        sb.append(s.charAt(0));
                        sb.append(s.charAt(1));
                        sb.append(s.charAt(2));
                        index--;
                        editText.setText(sb.toString());
                        editText.setSelection(index);
                        break;
                    case 9:
                        //183 2185 9
                        Log.i("9私聊","10");
                        sb.append(s.charAt(0));
                        sb.append(s.charAt(1));
                        sb.append(s.charAt(2));
                        sb.append(s.charAt(3));
                        sb.append(s.charAt(4));
                        sb.append(s.charAt(5));
                        sb.append(s.charAt(6));
                        sb.append(s.charAt(7));
                        index--;
                        editText.setText(sb.toString());
                        editText.setSelection(index);
                        break;
                }
            }
    
    
            return index;
        }
    

    activity里面调用:

    private TextWatcher numberWatcher =new TextWatcher() {
            private int changchu;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               changchu= ForNumber.mobileChanged(s,start,before,count,et_number,changchu);
                
            }
            @Override
            public void afterTextChanged(Editable s) {
    
            }
        };
    

    onTextChanged参数解释及实现EditText字数监听

    相关文章

      网友评论

          本文标题:EditText输入电话号码自带空格

          本文链接:https://www.haomeiwen.com/subject/cmdqmttx.html