对11位手机号进行3-4-4格式化
生活随笔
收集整理的這篇文章主要介紹了
对11位手机号进行3-4-4格式化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class PhoneNumFormatUtil {public static void phoneNumFormat(final EditText editText) {editText.addTextChangedListener(new TextWatcher() {int beforeTextLength = 0;int onTextLength = 0;boolean isChanged = false;int location = 0; // 記錄光標的位置private char[] tempChar;private StringBuffer buffer = new StringBuffer();int konggeNumberB = 0;public void onTextChanged(CharSequence s, int start, int before,int count) {// TODO Auto-generated method stubonTextLength = s.length();buffer.append(s.toString());if (onTextLength == beforeTextLength || onTextLength <= 3|| isChanged) {isChanged = false;return;}isChanged = true;}public void beforeTextChanged(CharSequence s, int start, int count,int after) {// TODO Auto-generated method stubbeforeTextLength = s.length();if (buffer.length() > 0) {buffer.delete(0, buffer.length());}konggeNumberB = 0;for (int i = 0; i < s.length(); i++) {if (s.charAt(i) == ' ') {konggeNumberB++;}}}public void afterTextChanged(Editable s) {// TODO Auto-generated method stubif (isChanged) {location = editText.getSelectionEnd();int index = 0;while (index < buffer.length()) {if (buffer.charAt(index) == ' ') {buffer.deleteCharAt(index);} else {index++;}}index = 0;int konggeNumberC = 0;while (index < buffer.length()) {if ((index == 3 || index == 8)) {buffer.insert(index, ' ');konggeNumberC++;}index++;}if (konggeNumberC > konggeNumberB) {location += (konggeNumberC - konggeNumberB);}tempChar = new char[buffer.length()];buffer.getChars(0, buffer.length(), tempChar, 0);String str = buffer.toString();if (location > str.length()) {location = str.length();} else if (location < 0) {location = 0;}editText.setText(str);Editable etable = editText.getText();Selection.setSelection(etable, location);isChanged = false;}}});}
}
?
轉載于:https://www.cnblogs.com/rh1910362960/p/4250323.html
總結
以上是生活随笔為你收集整理的对11位手机号进行3-4-4格式化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《Java程序员面试宝典》读书笔记1
- 下一篇: iPhone开发【一】从HelloWor