【问题标题】:Add character automatically in EditText after 6 letters6个字母后在EditText中自动添加字符
【发布时间】:2014-09-17 07:53:21
【问题描述】:

我想在我的 EditText 中的 6 个字母之后自动添加一个“-”,并且我希望用户可以在“-”之后继续书写。 (我希望用户写:1234562,它在 EditText 中显示为 123456-2)。

但我不知道该怎么做以及是否可能。如果你能帮助我,谢谢

【问题讨论】:

标签: android android-edittext


【解决方案1】:

添加textwatcher。然后使用以下代码:

@Override
public void afterTextChanged(Editable text) {     


    if (text.length() == 6) {
        text.append('-');
    }


}

您还可以在 if 语句中使用多个条件,例如:

 if (text.length() == 3 || text.length() == 6) {
        text.append('-');
    }

【讨论】:

    【解决方案2】:

    你也可以用这个来删除字符:

    text.addTextChangedListener(new TextWatcher() {
            int first = 0;
            int second;
    
            @Override
            public void afterTextChanged(Editable s) {
                second = first;
                first = s.length();
    
                if(text.length()==6 && first>second){
                    text.append("-");
                }
            }
    

    【讨论】:

    • 这应该是最好的答案。由于其他人不支持退格/删除。
    【解决方案3】:
    EditText editText = (EditText) findViewById(R.id.search);
    
    editText.addTextChangedListener(new TextWatcher() {          
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {                                   
    
    
                        }                       
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count,
                                int after) {
                            // TODO Auto-generated method stub                          
                        }                       
                        @Override
                        public void afterTextChanged(Editable text) {
                            // TODO Auto-generated method stub                          
                              if (text.length() == 6) {
                              text.append('-');
        }  
                        }
                    });
    

    【讨论】:

      【解决方案4】:
      EditText editText = (EditText) findViewById(R.id.editText1);
      editText.addTextChangedListener(new TextWatcher() {
          @Override
          public void afterTextChanged(Editable s) { 
              String text = editText.getText().toString(); 
              if(text.length() == 6){
                  editText.append("-");
              }
          }
      
          @Override
          public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
      
          }
      
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
      
          }
      });
      

      【讨论】:

      • 字符串文本 = editText.getText().toString(); if(text.length() == 6){ editText.append("-"); } 应该在 inTextChanged(...){}
      【解决方案5】:

      我使用了一些我找到的代码 here 来回答这个问题,它对我很有效,我真的希望这可以帮助其他人。这是针对这个特定问题修改的代码:

      myEditText.addTextChangedListener(new TextWatcher() {
          int count=0;
      
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {}
      
          @Override
          public void afterTextChanged(Editable s) {
      
              //Check if the text is 6 characters length, and if the user is writing.
              //If that condition is true then add a "-" to the end of the text and set the cursor to the last position
              //Otherwise if the text is 6 characters length, and if the user is deleting.
              //By removing the "-" you're actually removing the las digit as well, then the the cursor is set to the last position
      
              if (count <= myEditText.getText().toString().length() && s.length()==6){
      
                  String numberOnProgress = myEditText.getText().toString() + " ";
                  myEditText.setText(numberOnProgress);
                  int lastPosition = myEditText.getText().length();
                  myEditText.setSelection(lastPosition);
      
              }else if (count >= myEditText.getText().toString().length() && s.length()==6){
                  String numberOnProgress = myEditText.getText().toString().substring(0,myEditText.getText().toString().length()-1);
                  myEditText.setText(numberOnProgress);
                  int pos = myEditText.getText().length();
                  myEditText.setSelection(pos);
              }
              //Update the count value at the end
              count = myEditText.getText().toString().length();
          }
      });
      

      【讨论】:

      • 我忘了,我强烈建议将输入类型更改为“电话”,因为其他类型在尝试使用“”和“-”字符时可能会崩溃
      【解决方案6】:

      这是我对 Kotlin 的看法:

      codeInput.addTextChangedListener(object : TextWatcher {
          private var isFormatting = false
      
          override fun afterTextChanged(text: Editable) {
              if (isFormatting) {
                  return
              }
      
              isFormatting = true
      
              val formattedString = text
                  .replace("[^0-9]".toRegex(), "") // Keep only digits
                  .chunked(6) // Split into groups of 6
                  .joinToString("-") // Join the groups with hyphens
      
              text.clear()
              text.append(formattedString)
      
              isFormatting = false
          }
      
          override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
      
          override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-07
        相关资源
        最近更新 更多