在Android小部件之间实现发布订阅的本机方式是什么?

时间:2022-08-15 17:00:40

I have a custom EditText and a TextView in an Activity. The custom EditText traps for key presses and calculates the number of character entered and wants to publish the character count to the TextView. Should I use the java.util.Observable/Observer way? Or is there a more native android method to achive this?

我在Activity中有一个自定义的EditText和一个TextView。按键的自定义EditText陷阱,并计算输入的字符数,并希望将字符计数发布到TextView。我应该使用java.util.Observable / Observer方式吗?或者是否有一个更原生的android方法来实现这个?

1 个解决方案

#1


Observable/Observer is probably fine in the abstract.

Observable / Observer在摘要中可能很好。

For EditText/TextView, you could use addTextWatcher() rather than subclassing EditText to support your character count.

对于EditText / TextView,您可以使用addTextWatcher()而不是子类化EditText来支持您的字符数。

And bear in mind that you should really test this stuff with the soft keyboard, as I am under the impression that per-keystroke event behavior changes when users use the soft keyboard instead of the G1's QWERTY keyboard.

请记住,您应该使用软键盘测试这些内容,因为我觉得当用户使用软键盘而不是G1的QWERTY键盘时,每按键事件行为会发生变化。

#1


Observable/Observer is probably fine in the abstract.

Observable / Observer在摘要中可能很好。

For EditText/TextView, you could use addTextWatcher() rather than subclassing EditText to support your character count.

对于EditText / TextView,您可以使用addTextWatcher()而不是子类化EditText来支持您的字符数。

And bear in mind that you should really test this stuff with the soft keyboard, as I am under the impression that per-keystroke event behavior changes when users use the soft keyboard instead of the G1's QWERTY keyboard.

请记住,您应该使用软键盘测试这些内容,因为我觉得当用户使用软键盘而不是G1的QWERTY键盘时,每按键事件行为会发生变化。