无法插入可编辑
我一定在做一些显而易见的事情,但我不知道它是什么。我只是想将一个字符插入可编辑的:
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG, "inserting space at " + location);
s.insert(location, " ");
Log.d(TAG, "new word: '" + s + "'");
}
但 s 永远不会改变。字符串 's' 足够长,因为我打印它并且看起来不错。如果我调用 Editable.clear(),它会被清除,并且我可以使用 Editable.replace() 替换多个字符。有想法吗?
I must be doing something obvious, but I can't figure out what it is. I'm simply trying to insert a character into an Editable:
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG, "inserting space at " + location);
s.insert(location, " ");
Log.d(TAG, "new word: '" + s + "'");
}
But s never changes. The string 's' is long enough, because I print it and it looks good. If I call Editable.clear(), it is cleared, and I can replace multiple characters with Editable.replace(). Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现了问题;我将 inputType 设置为“number”,因此静默添加空格失败。
I found the problem; I set the inputType as "number" and so adding the space silently failed.
要使用输入过滤器编辑可编辑内容,只需保存当前过滤器,清除它们,编辑文本,然后恢复过滤器即可。
这是一些对我有用的示例代码:
To edit an editable with input filters, simply save the current filters, clear them, edit your text, and then restore the filters.
Here is some sample code that worked for me:
我的情况是,我想在输入邮政编码时在第三位插入“-”。 (例如 100-0001)。没有其他字符不允许进入。我在 xml 中设置了 EditText,
并在代码中添加了文本更改侦听器
通过这种方式我解决了我的问题...如果有其他更好的选项可用,请建议我其他方法...
My situation was, I want to insert a '-' at third place while typing the zip-code. (Eg. 100-0001). No other character not allowed to enter. I set my EditText in xml,
And in my code i add text change listener
By this way i solved my problem... Please suggest me other ways if there another better options available...
尝试:
Try: