replace character inside TextWatcher in android

帅比萌擦擦* 提交于 2019-12-21 17:06:54

问题


i use a TextWatcher to change pressed key value. my goal is that replace some characters while typing. for example when i type keys, if reached "S" character, replaces it with "a" character. my question is: should i do it in beforeTextChanged?? how? can anyone give me an example?


回答1:


Using beforeTextChanged won't be useful because it won't interrupt the actual printing of the key to the EditText. I would use something similar to:

    public void afterTextChanged(Editable s) {
        if(s.length() > 0 && s.toString().charAt(s.length()-1) == 'S')
        {
            final String newText = s.toString().substring(0, s.length()-1) + "a"; 
            editText.setText(newText); 
        }
    }

I added some toString()'s, not 100% sure how Editable works but I think that should cover it.




回答2:


I know that this post is a couple of years old, but both versions did not work for me and have build a hybrid between the two answers.

@Override
public void afterTextChanged(Editable editable) {    
    if (editable.toString().contains(",")) {
       Editable ab = new SpannableStringBuilder(editable.toString().replace(",", ""));
       editable.replace(0, editable.length(), ab);
    }
}



回答3:


You have to do it in the afterTextChanged, but don't forget to detach and reattach the TextChangedListener to prevent an endless loop.
A simple example is shown below :

public void afterTextChanged(Editable s) {
        editText.removeTextChangedListener(this);
        //.. do changes here ..//
        editText.setText(newText);
        editText.addTextChangedListener(this);
}

But there is another problem when you call setText, it causes the cursor to move to the end of the text inside the textView. So you have to calculate the new position for the curser yourself, remember user may enter the multiple characters at once by pasting or delete a selected part of the text.
Here is a more complete example. This watcher class removes all nonnumeric characters of the text.

public class NumWatcher implements TextWatcher {
    private EditText editText;
    private int selPos;
    private String oldString, newString;
    public NumWatcher(EditText editText) {
        this.editText = editText;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        selPos = editText.getSelectionStart();
        oldString = myFilter(s.toString());
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        editText.removeTextChangedListener(this);
        newString = myFilter(s.toString());
        editText.setText(newString);
        int newPos = selPos + (newString.length() - oldString.length());
        if (newPos < 0) newPos = 0;
        editText.setSelection(newPos);
        editText.addTextChangedListener(this);
    }

    public String myFilter(String s) {
        String digits;
        digits = s.replaceAll("[^0-9]", "");
        if (s.equals("")) return "";
        return digits;
    }
}



回答4:


@Override
public void afterTextChanged(Editable arg0) {
    Editable ab = new SpannableStringBuilder(arg0.toString().replace("S", "a"));
    arg0 = ab ;
}


来源:https://stackoverflow.com/questions/15653664/replace-character-inside-textwatcher-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!