在 afterTextChanged 事件中获取可编辑 ID [英] Get Editable id in afterTextChanged event
问题描述
我有一个活动,它扩展了 TextWatcher 来检测某些 EditTexts 中的变化,所以它实现了:
I have an Activity that extends TextWatcher to detect changes in certain EditTexts, so it implements:
public void afterTextChanged(Editable s)
我的问题是:如果有多个 EditTexts 设置了 .addTextChangedListener(this),那么在 afterTextChanged 过程中给定 Editable 对象的情况下,我如何区分哪一个发生了变化?
My question is: If there are several EditTexts with .addTextChangedListener(this) set, how can I differentiate which one changed given the Editable object in the afterTextChanged procedure?
推荐答案
另一个选项,使用较少的匿名内部类,将简单地检查当前聚焦的 View
.如果您的 TextWatcher
应用程序仅依赖于用户在键入时所做的更改,那么这些更改将始终发生在具有当前焦点的 View
中.从 Activity
或 Window
内部的任何位置调用 getCurrentFocus()
将返回用户关注的 View
.从 TextWatcher
内部,这几乎肯定是特定的 EditText
实例.
Another option, with fewer anonymous inner classes, would be to simply inspect the currently focused View
. If your application for TextWatcher
hinges solely on changes made by the user while typing, then the changes will always occur in the View
that has current focus. Calling getCurrentFocus()
from anywhere inside of an Activity
or Window
will return the View
the user is focused on. From inside a TextWatcher
, this will almost assuredly be the specific EditText
instance.
希望有帮助!
这篇关于在 afterTextChanged 事件中获取可编辑 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!