Android realizes the function of inputting mobile phone number space in EditText

  • 2021-08-12 03:47:23
  • OfStack

Android EditText Enter cell number space

The development requirement is to insert a space in the middle of the mobile phone EditText on the login page to make users look convenient, 130 123 4 456 7, and add a space before the space of the fourth and fifth numbers in the middle. There is another requirement, that is, in addition to entering the telephone number copied from other places, if there is no space, it should be automatically filled after pasting. Idea: Rewrite TextWatcher, and judge whether the content meets the requirements every time the content of EditText changes.

@Override
public void afterTextChanged(Editable s)
{
 // Requirements are 130 1234 4567 , middle number 4 Number and number 5 Number space preceded by space 
 StringBuffer sb = new StringBuffer(s);
 //StringBuffer.length() Is the length, so the subscript is from the 1 Begin 
 // Character array number 4 If the bit is not a space character, insert it before it 1 Space characters 
 if (s.length() >= 4)
 {
  char[] chars = s.toString().toCharArray();
  // Numeric subscripts are derived from 0 Begin 
  if (chars[3] != ' ')
  {
   sb.insert(3,' ');
   setContent(sb);
  }
 }
 if (s.length() >= 9)
 {
  char[] chars = s.toString().toCharArray();
  // Because the first 4 Bit added 1 A space, so the first 8 The bit number is the first of the character array 9 Bit, subscript is 8 . 
  if (chars[8] != ' ')
  {
   sb.insert(8,' ');
   setContent(sb);
  }
 }
}

Use, that is, to replace EditText content monitoring with self-written.


etPhone.addTextChangedListener(new PhoneTextWatcher(etPhone)
{
 @Override
 public void afterTextChanged(Editable s)
 {
  // If you still have to do monitoring things, you can continue to write 
  super.afterTextChanged(s);
 }
});

Complete code, which has one other person's method, on this method is pasted, blanks do not automatically complete:

https://www.ofstack.com/article/134183.htm


/**
 * Created by solexit04 on 2017/9/4.
 *  Insert a space in the middle of the mobile phone number 
 */
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);
  // Whether it is input status 
  isDelete = s.length() > lastContentLength ? false : true;
  // The input is the 4 , No. 9 Bit, when a space needs to be inserted 
  if (!isDelete && (s.length() == 4 || s.length() == 9))
  {
   if (s.length() == 4)
   {
    sb.insert(3, " ");
   } else
   {
    sb.insert(8, " ");
   }
   setContent(sb);
  }
  // Delete location to 4 , 9 Strip out spaces when 
  if (isDelete && (s.length() == 4 || s.length() == 9))
  {
   sb.deleteCharAt(sb.length() - 1);
   setContent(sb);
  }
  lastContentLength = sb.length();*/
 }
 @Override
 public void afterTextChanged(Editable s)
 {
  // Requirements are 130 1234 4567 , middle number 4 Number and number 5 Number space preceded by space 
  StringBuffer sb = new StringBuffer(s);
  //StringBuffer.length() Is the length, so the subscript is from the 1 Begin 
  // Character array number 4 If the bit is not a space character, insert it before it 1 Space characters 
  if (s.length() >= 4)
  {
   char[] chars = s.toString().toCharArray();
   // Numeric subscripts are derived from 0 Begin 
   if (chars[3] != ' ')
   {
    sb.insert(3,' ');
    setContent(sb);
   }
  }
  if (s.length() >= 9)
  {
   char[] chars = s.toString().toCharArray();
   // Because the first 4 Bit added 1 A space, so the first 8 The bit number is the first of the character array 9 Bit, subscript is 8 . 
   if (chars[8] != ' ')
   {
    sb.insert(8,' ');
    setContent(sb);
   }
  }
 }
 /**
  *  Add or remove spaces EditText Settings of 
  */
 private void setContent(StringBuffer sb)
 {
  editText.setText(sb.toString());
  // Move the cursor to the back 
  editText.setSelection(sb.length());
 }
}

Summarize


Related articles: