如何使用InputConnectionWrapper? [英] How to use InputConnectionWrapper?

查看:1983
本文介绍了如何使用InputConnectionWrapper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的EditText 。现在,我想获得的EditText 与他们的用户对所有这一切的变化和工作之前手动将它们插入到的EditText 。我不希望用户直接更改的EditText 的文本。这只能由我code来完成(通过使用更换如()的setText())。

I have an EditText. Now I want to get all changes made by the user to this EditText and work with them before manually inserting them into the EditText. I don't want the user to directly change the text in the EditText. This should only be done by my code (e.g. by using replace() or setText()).

我搜索了一下,发现了一个名为有趣的类 InputConnectionWrapper 。据Javadoc中应当充当一个给定的 InputConnection 代理。所以我子类它是这样的:

I searched a bit and found an interesting class named InputConnectionWrapper. According to the javadoc it shall act as a proxy for a given InputConnection. So I subclassed it like this:

private class EditTextInputConnection extends InputConnectionWrapper {

    public EditTextInputConnection(InputConnection target, boolean mutable) {
        super(target, mutable);
    }

    @Override
    public boolean commitText(CharSequence text, int newCursorPosition) {
                    // some code which takes the input and manipulates it and calls editText.getText().replace() afterwards
        return true;
    }

}

要初始化我重写下面的方法包装我的的EditText -subclass:

To initialize the wrapper I overwrote the following method in my EditText-subclass:

public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    InputConnection con = super.onCreateInputConnection(outAttrs);
    EditTextInputConnection connectionWrapper = new EditTextInputConnection(con, true);
    return connectionWrapper;
}

不过, commitText()永远不会被调用。该 onCreateInputConnection()被调用和构造 EditTextInputConnection 也有,但从来没有 commitText() ,altough应该是,当我输入一些文字进入该领域。至少,这就是我所理解的 InputConnectionWrapper 的使用。还是我错了?

However, commitText() never gets called. The onCreateInputConnection() gets called and the constructor of EditTextInputConnection also, but never commitText(), altough it should be, when I enter some text into the field. At least, that's how I understand the usage of InputConnectionWrapper. Or am I wrong?

编辑:看来,是 commitText()只要求特殊字符,如等。据我所知。 Android的源$ C ​​$ C对于所有其它字符 InputConnectionWrapper.sendKeyEvent()应该叫,但事实并非如此......我绝对停留在这一点上。我已经尝试过 EditText.onKey $ P $宗座外方传教会(),但在硬件键盘才可以。所以这是没有办法......我真的不明白,为什么Android的处理软键盘,不同的硬件键盘。 EditText.onTextChanged()也被解雇了非用户的输入,所以这也不是,我在寻找什么。

It seems, that commitText() is only called for special characters like "."," " etc. As I understand the Android sourcecode for all other characters InputConnectionWrapper.sendKeyEvent() should be called, but that's not the case... I'm absolutely stuck at this point. I already tried EditText.onKeyPreIme(), but this only works on hardware keyboards. So that's no alternative... I don't really understand, why Android handles soft keyboards that different from hardware keyboards. EditText.onTextChanged() gets also fired on non-user input, so this is also not, what I'm looking for.

推荐答案

原来,是的 InputConnectionWrapper 是完全正确的上述使用。然而, commitText()被永远不会调用(特殊情况除外),因为有其他的方法,这在打字过程中使用。这些主要是 setComposingText() sendKeyEvent()。然而,同样重要的是覆盖像很少使用方法 deleteSurroundingText() commitText(),以确保抓用户输入。

It turned out, that the above usage of the InputConnectionWrapper was totally correct. However, commitText() gets never called (except for special cases), as there are other methods, which are used during typing. These are mainly setComposingText() and sendKeyEvent(). However, it is also important to overwrite seldom used methods like deleteSurroundingText() or commitText() to make sure to catch every user input.

这篇关于如何使用InputConnectionWrapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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