十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章给大家分享的是有关如何实现Android手机号码输入框的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
创新互联建站主营濉溪网站建设的网络公司,主营网站建设方案,APP应用开发,濉溪h5微信小程序定制开发搭建,濉溪网站营销推广欢迎濉溪等地区企业咨询
具体代码如下所示:
package com.jixiong.teen.view; import android.content.Context; import android.text.Editable; import android.text.Selection; import android.text.TextWatcher; import android.util.AttributeSet; import android.widget.EditText; /** * Created by christy on 16/12/22. */ public class MoblieEditText extends EditText { public MoblieEditText(Context context) { super(context); this.addTextChangedListener(new MoblieWatcher()); } public MoblieEditText(Context context, AttributeSet attrs) { super(context, attrs); this.addTextChangedListener(new MoblieWatcher()); } public MoblieEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.addTextChangedListener(new MoblieWatcher()); } class MoblieWatcher implements TextWatcher { int beforeTextLength = 0; int onTextLength = 0; boolean isChanged = false; int location = 0;// 记录光标的位置 private char[] tempChar; private final StringBuffer buffer = new StringBuffer(); int konggeNumberB = 0; @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { beforeTextLength = 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++; } } } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { onTextLength = s.length(); buffer.append(s.toString()); if (onTextLength == beforeTextLength || onTextLength <= 3 || isChanged) { isChanged = false; return; } isChanged = true; } @Override public void afterTextChanged(Editable s) { if (isChanged) { location = 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; } setText(str); Editable etable = getText(); Selection.setSelection(etable, location); isChanged = false; } } } }
使用;;
直接在布局中引用
然后再activity中初始化
etUserNums.addTextChangedListener(new TeenEmptyWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (s != null && s.length() == 13) { if (etUserNums.isFocused()) { etUserNums.clearFocus(); etUserPwd.requestFocus(); } } } });
感谢各位的阅读!关于“如何实现Android手机号码输入框”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!