废话不多说了,直接给大家贴代码了,具体代码如下所示:
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; ikonggeNumberB) { 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手机号码输入框(满11位自动跳到下个输入框)实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!