private void editTextListener(){ id_num.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s == null || s.length() == 0) return; StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { if (i != 4 && i!= 9 && i != 14 && s.charAt(i) == ' ') { continue; } else { sb.append(s.charAt(i)); if ((sb.length() == 7 || sb.length() == 12|| sb.length() == 17) && sb.charAt(sb.length() - 1) != ' ') { sb.insert(sb.length() - 1, ' '); } } } if (!sb.toString().equals(s.toString())) { int index = start + 1; if (sb.charAt(start) == ' ') { if (before == 0) { index++; } else { id_num.setText(sb.subSequence(0, sb.length() - 1)); index--; } } else { if (before == 1) { index--; } } id_num.setText(sb.toString()); id_num.setSelection(index); } } @Override public void afterTextChanged(Editable s) { } }); }
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦