Android EditText如何在粘贴文本时禁用自动插入空格?

时间:2013-04-25 17:11:03

标签: android textedit

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

4 个答案:

答案 0 :(得分:0)

使用trim();它将从字符串

之前和之后删除空格
str = str.trim();

答案 1 :(得分:0)

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

它固定在5。

答案 2 :(得分:0)

我需要防止"空间"在使用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});

答案 3 :(得分: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。

旧的EditText看起来很奇怪看到TextWatcher。将字符串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
                }
            }
        });