I won’t say much nonsense, I will just post the code to you. The specific code is as follows:
package ; import ; import ; import ; import ; import ; import ; /** * Created by christy on 16/12/22. */ public class MoblieEditText extends EditText { public MoblieEditText(Context context) { super(context); (new MoblieWatcher()); } public MoblieEditText(Context context, AttributeSet attrs) { super(context, attrs); (new MoblieWatcher()); } public MoblieEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); (new MoblieWatcher()); } class MoblieWatcher implements TextWatcher { int beforeTextLength = 0; int onTextLength = 0; boolean isChanged = false; int location = 0;// Record the cursor position 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 = (); if (() > 0) { (0, ()); } konggeNumberB = 0; for (int i = 0; i < (); i++) { if ((i) == ' ') { konggeNumberB++; } } } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { onTextLength = (); (()); 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 < ()) { if ((index) == ' ') { (index); } else { index++; } } index = 0; int konggeNumberC = 0; while (index < ()) { if ((index == 3 || index == 8)) { (index, ' '); konggeNumberC++; } index++; } if (konggeNumberC > konggeNumberB) { location += (konggeNumberC - konggeNumberB); } tempChar = new char[()]; (0, (), tempChar, 0); String str = (); if (location > ()) { location = (); } else if (location < 0) { location = 0; } setText(str); Editable etable = getText(); (etable, location); isChanged = false; } } } }
use;;
Quote directly in the layout
< android: 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" />
Then initialize in the activity
(new TeenEmptyWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (s != null && () == 13) { if (()) { (); (); } } } });
Summarize
The above is the example code of the Android mobile phone number input box (the number of 11 digits will automatically jump to the next input box) introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!