无法插入可编辑

Sha*_*zon 10 android textwatcher

我必须做一些明显的事情,但我无法弄清楚它是什么.我只是想在可编辑中插入一个字符:

@Override
public void afterTextChanged(Editable s) {
    Log.d(TAG, "inserting space at " + location);
    s.insert(location, " ");
    Log.d(TAG, "new word: '" + s + "'");
}
Run Code Online (Sandbox Code Playgroud)

但是永远不会改变.字符串's'足够长,因为我打印它看起来很好.如果我调用Editable.clear(),它将被清除,我可以用Editable.replace()替换多个字符.想法?

Sha*_*zon 29

我发现了问题; 我将inputType设置为"number",因此静默添加空间失败.


Bec*_*caP 13

要使用输入过滤器编辑可编辑的内容,只需保存当前过滤器,清除它们,编辑文本,然后还原过滤器.

以下是一些适合我的示例代码:

@Override
public void afterTextChanged(Editable s) {
    InputFilter[] filters = s.getFilters(); // save filters
    s.setFilters(new InputFilter[] {});     // clear filters
    s.insert(location, " ");                // edit text
    s.setFilters(filters);                  // restore filters
}
Run Code Online (Sandbox Code Playgroud)