android format edittext to display spaces after every 4 characters

前端 未结 12 1685
傲寒
傲寒 2020-12-10 13:27

Android - I want to get a number input from the user into an EditText - it needs to be separated by spaces - every 4 characters. Example: 123456781234 -> 1234 5678 1234

12条回答
  •  伪装坚强ぢ
    2020-12-10 13:47

    change the live text while typing is some what difficult. we should handle the following issues.

    a. cursor position b. we should allow the user delete the entered text.

    The following code handle both the issues.

    1. Add TextWatcher to EditText, and get the text from "afterTextchanged()" and write your logic

      String str=""; int strOldlen=0;

          @Override
                  public void afterTextChanged(Editable s) {
      
         str = edtAadharNumber.getText().toString();
                      int strLen = str.length();
      
      
                      if(strOldlen 0) {
                              if (strLen == 4 || strLen == 9) {
      
                                  str=str+" ";
      
                                  edtAadharNumber.setText(str);
                                  edtAadharNumber.setSelection(edtAadharNumber.getText().length());
      
                              }else{
      
                                  if(strLen==5){
                                      if(!str.contains(" ")){
                                       String tempStr=str.substring(0,strLen-1);
                                          tempStr +=" "+str.substring(strLen-1,strLen);
                                          edtAadharNumber.setText(tempStr);
                                          edtAadharNumber.setSelection(edtAadharNumber.getText().length());
                                      }
                                  }
                                  if(strLen==10){
                                      if(str.lastIndexOf(" ")!=9){
                                          String tempStr=str.substring(0,strLen-1);
                                          tempStr +=" "+str.substring(strLen-1,strLen);
                                          edtAadharNumber.setText(tempStr);
                                          edtAadharNumber.setSelection(edtAadharNumber.getText().length());
                                      }
                                  }
                                  strOldlen = strLen;
                              }
                          }else{
                              return;
                          }
      
                      }else{
                          strOldlen = strLen;
      
      
                          Log.i("MainActivity ","keyDel is Pressed ::: strLen : "+strLen+"\n old Str Len : "+strOldlen);
                      }
      
                  }
      }
      
    2. Here I am trying to add space for every four characters. After adding first space, then the length of the text is 5. so next space is after 9 characters like that.

      if (strLen== 4||strLen==9)

      1. Here another problem is cursor position, once you modify the text of the edittext, the cursor move to first place. so we need to set the cursor manually.

      edtAadharNumber.setSelection(edtAadharNumber.getText().length());

      1. My text length is only 12 characters. So I am doing manual calculations, if your text is dynamic then you write dynamic logic.

提交回复
热议问题