【问题标题】:How to know a delete operation occurred in EditText in Android?如何知道 Android 的 EditText 中发生了删除操作?
【发布时间】:2020-02-09 11:01:48
【问题描述】:

我想在 EditText 中删除字符时获得回调。

我该怎么做?

【问题讨论】:

    标签: android android-edittext textwatcher


    【解决方案1】:

    没有直接删除字符的回调!!

    但是每次你添加任何文本或编辑你的EditText文本时,所有TextWatcher回调调用分别

    (1-beforeTextChanged, 2-onTextChanged, 3-afterTextChanged)

    因此,您可以检查所有这些中的删除操作,如下所示。 请注意,您不需要检查所有回调中的删除操作。 3 TextWatcher CallBacks中有3种方法可以理解TextWatcher中的删除操作,每种方法都可以解决您的问题:)

    。我认为您最好了解一些 TextWatcher 回调参数。

    正如@ikerfah所说的

    • start 是即将被删除的起始索引
    • count 是即将删除的文本长度
    • after 是即将添加的文本长度

    方式:

    1. beforeTextChanged:您比较 after 参数与 count 参数。
    2. onTextChanged:您在活动或片段中声明一个字段,并在每次onTextChanged 调用时填写该字段。比较你的 字段是 previous EditText countcount 参数是 当前EditTextCount;
    3. afterTextChanged: 很像 onTextChanged 监听器,只是你使用 length 而不是 count。

    在下面更改您的最终 addTextChangedListener 链接:

        yourEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
    
            if (after < count) {
                // delete character action have done
                // do what ever you want
                Log.d("MainActivityTag", "Character deleted");
               }
        }
    
        @Override
        public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
    
          //mPreviousCount count is fied
             if (mPreviousCount > count) {
                    // delete character action have done
                    // do what ever you want
                    Log.d("MainActivityTag", "Character deleted");
                }
                mPreviousCount=count;
            }
    
        @Override
        public void afterTextChanged(Editable editable) {
            Log.d("MainActivityTag",editable.toString());
            int length=editable.length();
    
          //mPreviousLength is a field
            if (mPreviousLength>length)
            {
                // delete character action have done
                // do what ever you want
                Log.d("MainActivityTag", "Character deleted");
            }
            mPreviousLength=length;
        }
     });
    

    【讨论】:

    • 这个解决方案不是一个好的解决方案。即使没有删除操作,您也可以得到更少的字符。例如,当您键入时,键盘会为您提供建议,而这些建议的长度可能会小于当前文本。所以你的解决方案会错误地将建议标记为删除案例。
    【解决方案2】:
    editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
            }
        });
    

    在 beforeTextChanged 回调中新建三个参数

    • start是即将被删除的起始索引
    • count 是即将被删除的文本的长度
    • 后面是即将添加的文本长度

    现在您只需在这些参数之间进行比较,即可通过 beforeTextChanged 回调实现您想要的任何目标

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 2021-02-10
      • 2019-09-05
      • 2021-12-09
      相关资源
      最近更新 更多