Android EditText enter mobile number auto space

  • Jane book: Android EditText enter cell phone number space
  • The development requirement is to insert a space in the middle of the mobile EditText on the login page to make the user look convenient. 130 1234 4567, the fourth number and the fifth number in the middle are preceded by a space.

  • It is also required that in addition to the input of the space to be added, the phone number copied from other places, if there is no space, the space should also be filled automatically after pasting.

  • Idea: Rewrite TextWatcher, and judge whether the content meets the requirements every time EditText content changes.

@Override
public void afterTextChanged(Editable s)
{
    //The requirement is 130 1234 4567, the fourth and fifth digit spaces in the middle are preceded by spaces
    StringBuffer sb = new StringBuffer(s);
    //StringBuffer.length() is the length, so the subscript starts from 1
    //If it is not a space character, insert a space character in front of it
    if (s.length() >= 4)
    {
        char[] chars = s.toString().toCharArray();
        //The number subscript starts at 0
        if (chars[3] != ' ')
        {
            sb.insert(3,' ');
            setContent(sb);
        }
    }

    if (s.length() >= 9)
    {
        char[] chars = s.toString().toCharArray();
        //Because a space is added to the fourth digit, the eighth digit is the ninth digit of the character array, and the subscript is 8.
        if (chars[8] != ' ')
        {
            sb.insert(8,' ');
            setContent(sb);
        }
    }
}
  • Use, is to change the EditText content monitor to write by yourself.
etPhone.addTextChangedListener(new PhoneTextWatcher(etPhone)
{
    @Override
    public void afterTextChanged(Editable s)
    {
        //If you need to do the monitoring work, you can continue to write
        super.afterTextChanged(s);
    }
});
  • The complete code contains a method of others. When pasting, the space will not be filled automatically:

http://blog.csdn.net/xiongge358/article/details/71750953

/**
 * Created by solexit04 on 2017/9/4.
 * Insert space between phone numbers
 */
 
public class PhoneTextWatcher implements TextWatcher
{
    private EditText editText;
    private boolean isDelete;
    private int lastContentLength;
 
    public PhoneTextWatcher(EditText editText)
    {
        this.editText = editText;
    }
 
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
    }
 
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
       /* StringBuffer sb = new StringBuffer(s);
        //Input status or not
        isDelete = s.length() > lastContentLength ? false : true;
 
        //The input is the 4th and 9th digit, in which case you need to insert a space
        if (!isDelete && (s.length() == 4 || s.length() == 9))
        {
            if (s.length() == 4)
            {
                sb.insert(3, " ");
            } else
            {
                sb.insert(8, " ");
            }
            setContent(sb);
        }
 
        //Remove the space when the deleted position reaches 4, 9
        if (isDelete && (s.length() == 4 || s.length() == 9))
        {
            sb.deleteCharAt(sb.length() - 1);
            setContent(sb);
        }
 
        lastContentLength = sb.length();*/
    }
 
    @Override
    public void afterTextChanged(Editable s)
    {
        //The requirement is 130 1234 4567, the fourth and fifth digit spaces in the middle are preceded by spaces
        StringBuffer sb = new StringBuffer(s);
        //StringBuffer.length() is the length, so the subscript starts from 1
        //If it is not a space character, insert a space character in front of it
        if (s.length() >= 4)
        {
            char[] chars = s.toString().toCharArray();
            //The number subscript starts at 0
            if (chars[3] != ' ')
            {
                sb.insert(3,' ');
                setContent(sb);
            }
        }
 
        if (s.length() >= 9)
        {
            char[] chars = s.toString().toCharArray();
            //Because a space is added to the fourth digit, the eighth digit is the ninth digit of the character array, and the subscript is 8.
            if (chars[8] != ' ')
            {
                sb.insert(8,' ');
                setContent(sb);
            }
        }
    }
 
    /**
     * Add or remove space EditText settings
     */
    private void setContent(StringBuffer sb)
    {
        editText.setText(sb.toString());
        //Move the cursor to the back
        editText.setSelection(sb.length());
    }
}

Keywords: Android Mobile

Added by needphphelp on Thu, 02 Apr 2020 09:34:15 +0300