【问题标题】:Avoiding backspace in EditText afterTextChanged() method在 EditText afterTextChanged() 方法中避免退格
【发布时间】:2020-01-14 06:41:51
【问题描述】:

所以,我实现了一个简单的替换方法,它在文本更改之前替换一个字符。

 @Override
 public void afterTextChanged(Editable s) {

     editText.removeTextChangedListener(this);

     if (s.length() == 0)
       return;
       s.replace(editText.getSelectionStart(),editText.getSelectionStart()+1,"PP");

     editText.addTextChangedListener(this);
}

由于这对所有类型的文本更改都有效,所以即使我按下退格键,它也会添加“PP”。有什么办法可以排除退格吗?

【问题讨论】:

    标签: android textwatcher addtextchangedlistener


    【解决方案1】:

    首先检测是否按下了退格键

    然后在按下退格键时避免替换

    editText.addTextChangedListener(new TextWatcher() {
    
            //Normally assume that Backspace not pressed 
            boolean isNotBackspace = true;
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //if current count is grater then before that means char added
                //So if current count is smaller then before that means backspace pressed 
                isNotBackspace = count>before;
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
                editText.removeTextChangedListener(this);
                Log.e("isNotBackButton", isNotBackButton+"");
    
                if (s.length() == 0) {
                    //Edit is here 
                    //return; //need to block return; because this preventing
                    //from adding  adding addTextChangedListener
                    isNotBackButton = true;
                //if not backspace pressed then proceed else no action 
                }else if(isNotBackButton){
                    s.replace(editText.getSelectionStart(), editText.getSelectionEnd(), "PP");
                }
    
                editText.addTextChangedListener(this);
    
            }
     });
    

    【讨论】:

    • 你的回答在某些时候是有效的,但问题是一旦所有输入的字符都被清除,s.replace() 就不会被执行。
    • 我并不是要对您的解决方案说任何坏话,只是指出它不起作用的漏洞。如果这样可以处理用户键入内容然后清除所有内容并重新键入的情况,我将很乐意接受此解决方案。
    • 你想说什么@AdhikariMadridistaSuman 如果你输入 abcd 然后按回来就会是 abcdPP ?
    • 如果再次输入关键字会发生什么
    • @Avinash 我的意思是在 Edittext 中,当用户输入内容并清除所有内容然后再次开始输入时,不会在每个输入的字符之后插入“pp”。
    【解决方案2】:

    您可以使用全局布尔变量来告诉您是否应该执行afterTextChanged() 中的代码。

    为此,您应该将View.onKeyListener() 添加到您的editText 并在其中添加thatBooleanVariable = keyCode != KeyEvent.KEYCODE_DEL;,如果单击退格键将返回false。

    之后,只需将 afterTextChanged() 中的所有内容都用 if (thatBooleanVariable) { } 包裹起来。

    【讨论】:

    • 根据文档 View.onKeyListener() 将对硬件键盘有用;软件输入法没有义务触发这个监听器。这里:developer.android.com/reference/android/view/View.OnKeyListener
    • 这适用于任何常规键盘,但它可能无法完全适用于 i.E.从 swiftkey 或带符号的键盘(中文键盘)滑动键盘
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2016-02-16
    • 2012-09-29
    • 2020-10-23
    • 2011-06-12
    • 2012-11-09
    • 1970-01-01
    相关资源
    最近更新 更多