【问题标题】:Android EditText How to disable automatic insertion of spaces when pasting text?Android EditText 粘贴文本时如何禁用自动插入空格?
【发布时间】:2013-04-25 17:11:03
【问题描述】:

有时,在 EditText 中粘贴文本时,会自动插入空格/空格。例如,如果将文本粘贴到文本字段中已包含的文本的中间或末尾,就会发生这种情况。有没有办法告诉 Edi​​tText 对象或 ClipboardManager 不应自动插入前导和尾随空格?

【问题讨论】:

标签: android textedit


【解决方案1】:

使用修剪();它将删除字符串前后的空格

str = str.trim();

【讨论】:

  • 我不知道在哪里添加 trim() 的调用。当我通过 ClipboardManager 检索剪贴板的内容时,找不到任何空白。我也尝试了一个 InputFilter 但这里过滤器方法被调用一次用于单个前导空白,一次用于尾随空白,一次用于实际剪贴板内容。在 InputFilter 中,我无法确定一个空白是来自用户还是这个 studip paste-method。
  • 似乎我不是唯一被这种自动化困扰的人:code.google.com/p/android/issues/detail?id=41037
  • 是的,我什至从未尝试过,我读过它并认为它适用于您的问题
【解决方案2】:

这是一个 Android 错误。 Check this issue report.

它在 5 中修复。

【讨论】:

    【解决方案3】:

    在使用 InputFilter 之前需要防止“空格”,所以我使用它:

        mSearchText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                MyLog.d(this, keyEvent);
                if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_SPACE) {
                    switch (keyEvent.getAction()) {
                        case KeyEvent.ACTION_DOWN:
                            mSpacePressed = true;
                            break;
                        case KeyEvent.ACTION_UP:
                            mSpacePressed = false;
                    }
                }
                return false;
            }
        });
    
        InputFilter mSearchFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence arg0, int arg1, int arg2, Spanned arg3, int arg4, int arg5)
            {
                //Prevent adding spaces on Paste
                //Remember: if you want paste " " it will be prevented
                if (arg0.toString().equals(" ")){
                    if (!mSpacePressed){
                        return "";
                    }
                }
    
                //do work with filter
    
                return null;
            }
        };
        mSearchText.setFilters(new InputFilter[]{mSearchFilter});
    

    【讨论】:

      【解决方案4】:

      我迟到了,但在我的旧平板电脑上,我的 EditText 也可以像你说的那样工作。

      所以,我像下面这样修复它。

      1.创建一个类来存储起始索引和插入字符串的长度信息。

      public class PasteUnit {
          int start = 0;
          int length = 0;
      
          public PasteUnit(int start, int length) {
              this.start = start;
              this.length = length;
          }
      }
      

      2.创建一个扩展EditText的类。

      public class MyEditText extends EditText
      {
          boolean pasteStarted = false;
          ArrayList<PasteUnit> pasteUnits = new ArrayList<>();
      
          public MyEditText(Context context)
          {
              super(context);
          }
      
          public MyEditText(Context context, AttributeSet attrs)
          {
              super(context, attrs);
          }
      
          public MyEditText(Context context, AttributeSet attrs, int defStyle)
          {
              super(context, attrs, defStyle);
          }
      
          @Override
          public boolean onTextContextMenuItem(int id) {
              if(id==android.R.id.paste) {//This is called when the paste is triggered
                  pasteStarted = true;
              }
      
              boolean consumed = super.onTextContextMenuItem(id);
              //the super.onTextContextMenuItem(id) processes the pasted string.
              //This is where EditText acts weird.
              //And we can watch what is happening in our TextWatcher to be added below
      
              switch (id){
                  case android.R.id.paste://This is called after we collected all the information
      
                      if(pasteUnits.size()>1) {//When a space or spaces are inserted
                          int startIndex = pasteUnits.get(0).start;
                          int totalLength = 0;
                          for(int i=0;i<pasteUnits.size();i++) {
                              PasteUnit pasteUnit = pasteUnits.get(i);
                              totalLength = totalLength + pasteUnit.length;
                          }
                          int endIndex = startIndex + totalLength;
      
                          String string = this.getText().toString();
                          String before = string.substring(0, startIndex);
                          String after = string.substring(endIndex);
      
                          PasteUnit lastPasteUnit = pasteUnits.get(pasteUnits.size()-1);
                          String lastString = string.substring(lastPasteUnit.start, lastPasteUnit.start + lastPasteUnit.length);
      
                          String result = before + lastString + after;
                          this.setText(result);
                          this.setSelection(startIndex + lastString.length());
                      }
                      pasteUnits.clear();
                      pasteStarted = false;
                      break;
                  case android.R.id.copy:
                      break;
                  case android.R.id.cut:
                      break;
              }
              return consumed;
          }
      }
      

      3.将 TextWatcher 添加到您的 EditText。

      看到 TextWatcher,旧的 EditText 的行为真的很奇怪。当您将字符串 A 粘贴到字符串 B 中时,它首先在字符串 B 中插入一个空格,然后在字符串 B 中插入另一个空格,最后在两个空格之间插入字符串 A。

      在其他情况下,例如在字符串 B 之后粘贴字符串 A,它首先在字符串 B 之后附加一个空格,然后再附加字符串 A。

      所以无论如何它似乎在最后一步插入原始字符串。

         MyEditText edit = (MyEditText) findViewById(R.id.mainEditText1);
      
         edit.addTextChangedListener(new TextWatcher() {
      
                  public void afterTextChanged(Editable s) {
                  }
      
                  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                  }
      
                  public void onTextChanged(CharSequence s, int start, int before, int count) {
      
                      if(edit.pasteStarted) {//when it is processing what we pasted
                          edit.pasteUnits.add(new PasteUnit(start, count));
                         //store the information about the inserted spaces and the original pasted string
                      }
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-24
        • 1970-01-01
        • 2011-09-10
        • 1970-01-01
        • 1970-01-01
        • 2022-06-23
        相关资源
        最近更新 更多