您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)如何實(shí)現(xiàn)Android手機(jī)號碼輸入框的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
具體代碼如下所示:
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;// 記錄光標(biāo)的位置 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; } } } }
使用;;
直接在布局中引用
<com.jixiong.teen.view.MoblieEditText android:id="@+id/etUserNums" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:hint="@string/user_name" android:inputType="number" android:maxLines="1" android:paddingLeft="@dimen/margin_twenty" android:singleLine="true" android:textColorHint="@color/hint_color" android:textSize="@dimen/sp_14" />
然后再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(); } } } });
感謝各位的閱讀!關(guān)于“如何實(shí)現(xiàn)Android手機(jī)號碼輸入框”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。