EditText上:由的setText(文字变化)或通过键盘输入之间的区分 [英] EditText: Differentiate between text change by setText() or by keyboard input

查看:1996
本文介绍了EditText上:由的setText(文字变化)或通过键盘输入之间的区分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的EditText 查看这是从我的code和由主编的setText()通过(软/硬)键盘和用户 - 如果可以通过语音输入。我想处理输入以另一种方式比普通用户的输入由我code:如果用户输入的情况发生,一个内部事件应被解雇。但我不知道如何在两者之间区分。 previously - 在模拟器上进行开发时 - 我使用了的onkeydown()办法赶上用户输入。然而,在真实设备上进行测试时,我发现,那的onkeydown()不是从软键盘输入触发。另外语音输入将不会被这个公认的,但我认为这是一个小小的瑕疵。这样的解决方案是为我别无选择。

I have an EditText View which is edited by setText() from my code and by the user via (soft/hard) keyboard and - if possible by speech input. I want to handle input made by my code in another way than normal user input: if a user input happens, a internal event shall be fired. But I don't know how to differentiate between both. Previously - when developing on the emulator - I used the onKeyDown() method to catch user input. However, when testing on a real device, I found out, that onKeyDown() isn't triggered from soft keyboard input. Additionally speech input wouldn't be recognized by this, though I consider this to be a minor flaw. So that solution is no option for me.

在另一方面还有就是 onTextChanged()方法,但是这是由两个触发的setText()和键盘输入。那么,如何区分这两种或方法仅被用户输入的,而不是在使用的setText(),这样我就可以覆盖它?

On the other hand there is the onTextChanged() method, but this is triggered by both setText() and keyboard input. So how can I differentiate between both or which method is only called by user input, but not when using setText(), so can I overwrite it?

推荐答案

我终于通过实施解决了这个问题一个 InputConnectionWrapper (见<一href="http://stackoverflow.com/questions/6626283/how-to-use-inputconnectionwrapper/6707451#6707451">this问题,特别是用于一个实施例的答案),其中有各种方法来从一个软键盘获得输入。我回到我的 InputConnectionWrapper 的方法 EditText.onCreateInputConnection()。对于硬键盘,我用 EditText.on $ P $宗座外方传教会()。所有这些方法都是通过我的框架,处理文本输入并且相应地更新视图覆盖和路由它们的输入。这意味着,在所有这些被覆盖的方法(除了 onCreateInputConnection())的超级方法的没有的叫,因为我更新视图自己。我的数据模型和视图之间的这种prevents不一致。

I finally solved the problem by implementing a InputConnectionWrapper (see this question and particularly the answer for an implementation example) which has various methods to get the input from a soft-keyboard. I return my InputConnectionWrapper in the method EditText.onCreateInputConnection(). For hard keyboards I use EditText.onPreIme(). All these methods are overwritten and route their input through my framework which handles the text input and updates the View accordingly. This means, in all these overwritten methods (except for onCreateInputConnection()) the super method is not called cause I update the View myself. This prevents inconsistencies between my data model and the View.

这篇关于EditText上:由的setText(文字变化)或通过键盘输入之间的区分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆