2013-04-25 91 views
3

有時,在EditText中粘貼文本時,會自動插入空格/空格。例如,如果將文本粘貼到文本字段中已包含的文本的中間或末尾,就會發生這種情況。有沒有辦法告訴EditText對象或ClipboardManager不應該自動插入前導空白和尾隨空白?Android EditText如何在粘貼文本時禁用自動插入空格?

+1

我無法確認此行爲。也許這是你的鍵盤?你有沒有嘗試過Android鍵盤? – 2013-04-25 17:13:54

+1

你可以只刪除它們的代碼喜歡這裏: http://stackoverflow.com/questions/3349121/how-do-i-use-inputfilter-to-limit-characters-in-an-edittext- in-android – user1875797 2013-04-25 17:20:30

回答

0

使用trim();它會從字符串前後刪除空格

str = str.trim(); 
+1

我不知道在哪裏添加trim()的調用。當我通過ClipboardManager檢索剪貼板的內容時,不能找到空格。我也嘗試了一個InputFilter,但是這裏的過濾器方法只針對一個前導空白,一個針對尾隨空白,一次針對實際剪貼板內容。在InputFilter中,我無法決定一個空白是來自用戶還是這個studip粘貼方法。 – EricJobs 2013-04-29 14:21:46

+1

好像我不是唯一一個被這種自動主義困擾的人:http://code.google.com/p/android/issues/detail?id=41037 – EricJobs 2013-04-29 14:32:38

+0

是啊我從來沒有嘗試過這個,我讀過它,並認爲它將適用於你的問題 – JRowan 2013-04-29 16:49:59

0

我需要防止的「空間」之前確實與輸入過濾工作,所以我用的就是:

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}); 
0

我來晚了,但在我的老片,我的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之後粘貼字符串中的其他情況下,首先附加字符串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 
       } 
      } 
     }); 
相關問題