Android格式化手机号码为xxx xxxx xxxx

((EditText) realView_).addTextChangedListener(new TextWatcher() {

private boolean isDelete;

@Override

public void onTextChanged(CharSequence s, int cursorPosition, int before, int count) {

((EditText) realView_).setOnKeyListener(new OnKeyListener() {

@Override

public boolean onKey(View v, int keyCode, KeyEvent event) {

//是否按下了delete键

if (keyCode == KeyEvent.KEYCODE_DEL) {

  isDelete = true;

      }

return false;

}

});

//有*号覆盖的账号具有一键删除功能,mask_为标记是否有星号的属性

if (null != mask_) {

                if (count > 1) {

                    textChanged_ = false;

                } else if (!textChanged_) {

                    textChanged_ = true;

                    if (count == 1) {

                        s = s.subSequence(cursorPosition, cursorPosition + 1);

                    } else {

                        s = "";

                    }

                    ((EditText) realView_).setText(s.toString());

                }

            }

UserUtils.formatPhoneNumber(s, cursorPosition, before, count, ((EditText) realView_), this);

}



@Override

public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3) {

}

@Override

public void afterTextChanged(Editable s) {

// TODO Auto-generated method stub

}

});

/**

 * 格式化手机号XXX XXXX XXXX

 * @author devloper

 *

 */

public static class UserUtils{

public static void formatPhoneNumber(CharSequence s, int cursorPosition, int before, int count,EditText mEditText,TextWatcher mTextWatcher){

if(before == 0 && count == 1){  //Entering values


String val = s.toString();

String a = "";

String b = "";

String c = "";

if (val != null && val.length() > 0) {

    val = val.replace(" ", "");

    if (val.length() >= 3) {

        a = val.substring(0, 3);

    } else if (val.length() < 3) {

        a = val.substring(0, val.length());

    }

    if (val.length() >= 7) {

        b = val.substring(3, 7);

        c = val.substring(7, val.length());

    } else if (val.length() > 3 && val.length() < 7) {

        b = val.substring(3, val.length());

    }

    StringBuffer stringBuffer = new StringBuffer();

    if (a != null && a.length() > 0) {

        stringBuffer.append(a);

    }

    if (b != null && b.length() > 0) {

        stringBuffer.append(" ");

        stringBuffer.append(b);

    }

    if (c != null && c.length() > 0) {

        stringBuffer.append(" ");

        stringBuffer.append(c);

    }

    mEditText.removeTextChangedListener(mTextWatcher);

    mEditText.setText(stringBuffer.toString());

    if(cursorPosition == 3 || cursorPosition == 8){

        cursorPosition = cursorPosition+2;

    }else{

        cursorPosition = cursorPosition+1;

    }

    if(cursorPosition <= mEditText.getText().toString().length()) {

    mEditText.setSelection(cursorPosition);

    }else{

    mEditText.setSelection(mEditText.getText().toString().length());

    }

    mEditText.addTextChangedListener(mTextWatcher);

} else {

mEditText.removeTextChangedListener(mTextWatcher);

mEditText.setText("");

mEditText.addTextChangedListener(mTextWatcher);

}

}

if(before == 1 && count == 0){  //Deleting values

String val = s.toString();

String a = "";

String b = "";

String c = "";

if (val != null && val.length() > 0) {

    val = val.replace(" ", "");

    if(cursorPosition == 3){

        val = removeCharAt(val,cursorPosition-1,s.toString().length()-1);

    }else if(cursorPosition == 8){

        val = removeCharAt(val,cursorPosition-2,s.toString().length()-2);

    }

    if (val.length() >= 3) {

        a = val.substring(0, 3);

    } else if (val.length() < 3) {

        a = val.substring(0, val.length());

    }

    if (val.length() >= 7) {

        b = val.substring(3, 7);

        c = val.substring(7, val.length());

    } else if (val.length() > 3 && val.length() < 7) {

        b = val.substring(3, val.length());

    }

    StringBuffer stringBuffer = new StringBuffer();

    if (a != null && a.length() > 0) {

        stringBuffer.append(a);

    }

    if (b != null && b.length() > 0) {

        stringBuffer.append(" ");

        stringBuffer.append(b);

    }

    if (c != null && c.length() > 0) {

        stringBuffer.append(" ");

        stringBuffer.append(c);

    }

    mEditText.removeTextChangedListener(mTextWatcher);

    mEditText.setText(stringBuffer.toString());

    if(cursorPosition == 3 || cursorPosition == 8){

        cursorPosition = cursorPosition-1;

    }

    if(cursorPosition <= mEditText.getText().toString().length()) {

    mEditText.setSelection(cursorPosition);

    }else{

    mEditText.setSelection(mEditText.getText().toString().length());

    }

    mEditText.addTextChangedListener(mTextWatcher);

} else {

mEditText.removeTextChangedListener(mTextWatcher);

mEditText.setText("");

mEditText.addTextChangedListener(mTextWatcher);

}

}

}

/**

* 光标在空格前面时,删除空格及空格前面一位

*/

public static String removeCharAt(String s, int pos,int length) {

String value = "";

if(length > pos){

value = s.substring(pos + 1);

}

return s.substring(0, pos)+value ;

}

}


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