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 ;

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Vue 3 中的 Composition API 对输入框进行格式化的代码示例: ```vue <template> <div> <label for="phone">Phone:</label> <input id="phone" v-model="formattedPhone" @input="onPhoneInput" /> </div> </template> <script lang="ts"> import { ref, watch } from 'vue'; export default { setup() { const phone = ref(''); const formattedPhone = ref(''); const formatPhone = (value: string) => { const regex = /^(\d{0,3})(\d{0,3})(\d{0,4})$/; const match = value.match(regex); if (match) { formattedPhone.value = `(${match[1]}) ${match[2]}-${match[3]}`; } else { formattedPhone.value = value; } }; const onPhoneInput = (event: InputEvent) => { const target = event.target as HTMLInputElement; const value = target.value.replace(/\D/g, ''); phone.value = value; }; watch(phone, (value: string) => { formatPhone(value); }); return { phone, formattedPhone, onPhoneInput, }; }, }; </script> ``` 解释一下: - 我们使用 Vue 3 中的 Composition API 来编写组件。 - `phone` 和 `formattedPhone` 都是响应式变量,分别保存未格式化格式化后的电话号码。 - `formatPhone` 函数接受一个字符串参数 `value`,将其格式化为 `(xxx) xxx-xxxx` 的电话号码,并将其保存在 `formattedPhone` 变量中。 - 正则表达式 `^(\d{0,3})(\d{0,3})(\d{0,4})$` 匹配一个最多十位数字的电话号码,其中: - `^` 表示字符串的开始。 - `(\d{0,3})` 表示一个至多三位数字的组。 - `(\d{0,3})` 表示另一个至多三位数字的组。 - `(\d{0,4})` 表示一个至多四位数字的组。 - `$` 表示字符串的结尾。 - 如果 `value` 符合正则表达式的格式,则使用 `match` 方法将其分成三个组,并返回格式为 `(xxx) xxx-xxxx` 的电话号码。 - 如果 `value` 不符合正则表达式的格式,则将其保存在 `formattedPhone` 变量中。 - `onPhoneInput` 函数接受一个 `InputEvent` 参数 `event`,将输入框的值 `value` 替换为仅包含数字的字符串,并将其保存在 `phone` 变量中。 - `watch` 函数监听 `phone` 变量的变化,并在变化时调用 `formatPhone` 函数来格式化电话号码。 - 最后,在模板中使用 `v-model` 指令将 `formattedPhone` 绑定到输入框上,并使用 `@input` 事件监听输入框的输入。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值