了解编辑文本何时完成编辑
我如何知道我的编辑文本何时完成编辑?就像用户选择下一个框或按软键盘上的完成按钮时一样。
我想知道这一点,以便我可以限制输入。看起来文本观察器的 afterTextChanged 发生在输入每个字符之后。我需要对输入进行一些计算,因此我想避免在输入每个字符后进行计算。
谢谢
How do I know when my edit text is done being edited? Like when the user selects the next box, or presses the done button on the soft keyboard.
I want to know this so I can clamp the input. It looks like text watcher's afterTextChanged happens after each character is entered. I need to do some calculations with the input, so I would like to avoid doing the calculation after each character is entered.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通过使用这样的东西
By using something like this
EditText
继承setOnFocusChangeListener
,它采用OnFocusChangeListener
的实现。实现
onFocusChange
并且有一个hasFocus
的布尔参数。如果这是错误的,您就会失去对另一个控件的关注。编辑
要处理这两种情况 - 编辑文本失去焦点或用户单击“完成”按钮 - 创建一个从两个侦听器调用的方法。
EditText
inheritssetOnFocusChangeListener
which takes an implementation ofOnFocusChangeListener
.Implement
onFocusChange
and there's a boolean parameter forhasFocus
. When this is false, you've lost focus to another control.EDIT
To handle both cases - edit text losing focus OR user clicks "done" button - create a single method that gets called from both listeners.
使用这样定义的 EditText 对象 xml:
我们可以捕获其文本 i) 当用户单击软键盘上的“完成”按钮 (OnEditorActionListener) 或 ii) 当 EditText 失去用户焦点 (OnFocusChangeListener)(现在位于另一个 EditText 上) :
这对我有用。您可以使用如下代码进行计算后隐藏软键盘:
编辑:向onEditorAction添加返回值
Using an EditText object xml defined like this:
We can capture its text i) when the user clicks the Done button on the soft keyboard (OnEditorActionListener) or ii) when the EditText has lost the user focus (OnFocusChangeListener) which now is on another EditText:
This works for me. You can hide the soft keyboard after made the calculations using a code like this:
Edit: added return values to onEditorAction
我做了这个简单的事情,当焦点从编辑文本转移时获取文本。如果用户将焦点转移到选择其他视图(例如按钮或其他 EditText 或任何视图),这将起作用。
I have done this simple thing, when focus is shifted from edit text get the text. This will work if user shifts the focus to select other views like a button or other EditText or any view.
要更高级,您可以使用 TextWatcher 的 afterTextChanged () 方法。
To be more highlevel you may use TextWatcher's afterTextChanged() method.