如何使用 textwatcher 从 edittext 获取当前键入的文本?

How to get the currently typed text from edittext using textwatcher?

例如:如果我输入 "hello sir",那么在我开始输入 "sir" 使用 textwatcher 在 android。 当用户点击 space 栏时,我需要当前从 edittext 中输入单个文本,而不是 edittext 中的所有字符串。 任何人都可以使用 textwatcher 提供可靠的想法吗?

public void onTextChanged(CharSequence charsequence, int i, int j, int k) {
                // TODO Auto-generated method stub
                // I need only currently typed text not all the string from edittext
            }
        });

只要调用 onTextChanged 就会得到整个字符串,没有 "when spacebar is pressed" 回调,因此您必须自己编写。

一个想法可能如下:
在 onTextChanged() 中,检查字符串中是否有任何 space,如果找到,遍历字符串,将字符串拆分为 space 个分隔字符串的数组。
然后你把每个字符串都放在一个数组中,然后简单地找到你需要的那个。

这会将字符串 "How are you doing today?" 转换为 "How" "are" "you" "doing" "today?" 的数组,然后是一个简单的数组索引的情况来找到你需要的字符串。