手机号间隔生成

本文介绍了一种用于格式化用户输入的实用工具类,能够根据不同类型的输入(如身份证号、银行卡号等)自动插入空格以改善可读性,并提供了一个监听器来实时更新显示格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、之前在网上找的一篇也是生成间隔的,但是,同时一个edittext 监听两次,里面的方法便会运行两次。无奈之下,又重新找了一个,代码图下:

public class DealSpaceUtils {

    public  static  String  formateBankno(String source,int tag) {

        StringBuffer buffer = new StringBuffer(source.replace(" ", ""));
        switch (tag) {
            case 0:                 //身份证号
                try {
                    buffer.insert(6, " ");
                    buffer.insert(15, " ");
                    buffer.insert(20, " ");
                    buffer.insert(24, " ");
                } catch (Exception e) {
                    // TODO: handle exception
                }
                return buffer.toString();

            case 1101:                 //身份证号
                try {
                    buffer.insert(3, " ");
                    buffer.insert(7, " ");
                    buffer.insert(12, " ");
                    buffer.insert(17, " ");
                } catch (Exception e) {
                    // TODO: handle exception
                }
                return buffer.toString();

            case 1:
                                //卡号
                try {
                    buffer.insert(4, " ");
                    buffer.insert(9, " ");
                    buffer.insert(14, " ");
                    buffer.insert(19, " ");
                } catch (Exception e) {
                    // TODO: handle exception
                }
                return buffer.toString();

            case 2:   //3, 8, 13, 18     手机号

                try {
                    buffer.insert(3, " ");
                    buffer.insert(8, " ");
                } catch (Exception e) {
                    // TODO: handle exception
                }
                return buffer.toString();

            default:
                return null;
        }
    }


    public  static  void  watcherListener(final EditText editText, final int tag) {


          editText.addTextChangedListener(new TextWatcher() {

              int count;

              @Override
              public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
              }
              @Override
              public void onTextChanged(CharSequence charSequence, int start, int  before, int count) {
                  this.count = count;
              }
              @Override
              public void afterTextChanged(Editable s) {
                  String result;



                      result = formateBankno(s.toString(),tag);

//
                  resultInfo( s, result, count, editText, tag);
              }
          });
     }

    public static void  resultInfo (Editable s,String result,int count,EditText editText,int tag) {

        if (result.equals(s.toString())) {

            return;
        }


        int index;
        if (count==0) {
            index = editText.getSelectionStart();
        }
        else {

            index = editText.getSelectionStart();

            if(tag == 0) {    //身份证号

                if (index ==7||index ==16) {
                    index+=1;
                }
            }else if(tag == 1) {    //卡号   4, 9, 14, 19

                if (index ==5||index ==10||index ==15 || index == 20) {
                    index+=1;
                }
            } else if (tag == 2) {   //手机号   3, 8, 13, 18

                if (index == 3 || index == 8 ) {
                     index +=1 ;
                }
            } else if (tag == 1101) {//  111  111  1111  1111  1111
                if (index == 3 || index == 7 ||index == 12 || index == 17) {
                    index+=1;
                }
            }
            }
        editText.setText(result);
        editText.setSelection(index);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值