用于多个 EditText 的 TextWatcher
我想为多个 EditText
字段实现 TextWatcher
接口。目前我正在使用 :
text1.addTextChangedListener(this);
text2.addTextChangedListener(this);
then 覆盖我的 Activity 中的方法:
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// do some operation on text of text1 field
// do some operation on text of text2 field
}
但是这工作正常,但我正在寻找其他方法,以便我可以明确识别 EditText
字段中的 SoftKeyboard< /code> 当前处于焦点状态。
I want to implement the TextWatcher
interface for more than one EditText
fields. Currently I am using :
text1.addTextChangedListener(this);
text2.addTextChangedListener(this);
then overriding the methods in my Activity:
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// do some operation on text of text1 field
// do some operation on text of text2 field
}
However this is working fine but I'm looking for other ways so that I can explicitly identify that in which EditText
field the SoftKeyboard
is currently focused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
@Sebastian Roth 的回答中建议的解决方案不是某些
EditText 的
。对于 n 个TextWatcher
实例EditText
,它是一个类和该类的 n 个实例。每个 EditText 都有自己的 Spannable。
TextWatcher
的事件将此 Spannable 作为s
参数。我检查它们的 hashCode(每个对象的唯一 ID)。 myEditText1.getText() 返回 Spannable。因此,如果myEditText1.getText().hashCode()
等于s.hashCode()
则意味着s
属于myEditText1
因此,如果您想为某些
EditTexts
拥有一个TextWatcher
实例,您应该使用以下命令:
Suggested solution in @Sebastian Roth's answer is not one instance of
TextWatcher
for someEditTexts
. It is one class and n instances of that class for nEditTexts
.Each EditText has its own Spannable.
TextWatcher
's events has this Spannable ass
parameter. I check their hashCode (unique Id of each object). myEditText1.getText() returns that Spannable. So if themyEditText1.getText().hashCode()
equals withs.hashCode()
it means thats
belongs tomyEditText1
So if you want to have one instance of
TextWatcher
for someEditTexts
you should use this:and
我会这样做:
I would do it like this:
我使用这个解决方案:
添加返回侦听器的方法:
为多个EditText添加监听器,也可以传递其他参数:
I use this solution:
Add method that returns listener:
Add listener to multiple EditText's, you can also pass other parameters:
使用“CustomTextWatcher”的想法,我做到了
1)创建了一个新的TextWatcherListener接口:
2)创建并使用EditTextExtended而不是EditText(在我的例子中):
3)所以,你需要编写此代码的地方:
4)使用它们:
嗯,让我知道你的想法。
using "CustomTextWatcher" idea, I done that
1) Crated a new TextWatcherListener interface:
2)Created and used EditTextExtended instead of EditText (in my case):
3) So, where you need write this code:
4)use them:
Well, let me know what do you think.
--EDIT--
如果您只想使用 afterTextChanged 比较可编辑内容:
--EDIT--
If you want to use only afterTextChanged compare editables:
我将其实现为:
和:
I implemented it as:
and:
另一种方法是将
OnClickListener
添加到EditText
并设置一个全局变量,如下所示使用此 etCurrentEditor 作为对当前编辑的 EditText 的引用
One more way around is to add
OnClickListener
toEditText
and set a global variable as given belowUse this etCurrentEditor as a reference to currently edited EditText
在尝试了多种方法来实现这一目标之后,我找到了使用
EditText.isFocused()
区分彼此的正确方法。例如:After try several ways to achieve this, i find the right way using
EditText.isFocused()
to distinguish one to another. For example:是的,您可以使用存储
TextView
的自定义TextWatcher
的多个实例。(
TextView
实际上是具有addTextChangedListener
的类。)与上面的 hashCode 解决方案类似,您只需检查是否
getText()==s
即可。您无需多次存储所有控件或
findViewById
,只需自行扫描一次内容树即可查找具有CharSequence
的控件。当然,您也可以在
beforeTextChanged
和onTextChanged
中使用findTextView
。Yes, you could use multiple instances of a custom
TextWatcher
that store theTextView
.(
TextView
is actually the class that hasaddTextChangedListener
.)Similar to the hashCode solution above you can just check if
getText()==s
.Instead of either storing all your controls or
findViewById
multiple times, you could simply scan the content tree yourself once for the control that has theCharSequence
.Of course you could also use
findTextView
insidebeforeTextChanged
andonTextChanged
.所有活动的全球一类。
CustomTextWatcher.java
Global One class for all the activities.
CustomTextWatcher.java
您可以执行此操作来获取编辑文本的 ID。它尚未经过测试,但请告诉我它是否有效。
You can do this for getting the id of the edit texts. It's not tested but let me know if it works.
您始终可以将
TextWatcher
定义为addTextChangedListener
方法的参数。这样您就可以为每个编辑文本拥有多个定义。You can always define
TextWatcher
as a parameter toaddTextChangedListener
method.This way you can have multiple definitions for each edit text.只需使用 hashCode() 方法比较 edittext 和字符串的哈希码
just compare hash codes of the edittext and string like by using hashCode() method
这就是我所做的...
然后只需将 TextWatcher 添加到 onCreate 方法中的每个 EditText 中即可这里还默认保留了按钮 setEnabled(false) 。
This is what I have done...
Then just added the TextWatcher to each EditText in the onCreate method & also kept the button setEnabled(false) by default here.
如果您使用 Kotlin,扩展函数就可以完成这项工作。
例如,我们需要将 TextWatcher 添加到
editText1
和editText2
创建一个这样的扩展函数,
然后将 textwatcher 添加到 EditTexts 中,如下所示
If you are using Kotlin, extension function will do the job.
For example, we need to add TextWatcher to
editText1
andeditText2
Create a extension function like this,
Then just add the textwatcher to EditTexts like this
我做了这样的事情,只有一个
TextWatcher
类来控制来自Activity
或Fragment
的尽可能多的EditText
>您需要首先创建一个
MultiTextWatcher
类,如下所示,然后在您的
Activity
或Fragment
中,您需要注册尽可能多的EditText< /code> 如您所愿,如下所示,而且我还使用了 数据绑定 获取
XML
视图的引用,您可以使用您的方式。I've done something like this to have only one
TextWatcher
class to control as manyEditText
either fromActivity
ORFragment
You need to first create a
MultiTextWatcher
class as belowThen in your
Activity
orFragment
, you need to register as manyEditText
as you want like below, and also I've used Data Binding to get references of theXML
views and you can use your ways.使用 :
using :