简体   繁体   English

以编程方式显示软键盘时如何捕捉按键事件?

[英]How to catch key event when soft keyboard is shown programmatically?

I'm trying to write text in Canvas . 我正在尝试在Canvas编写文本。 As I need to show the soft keyboard to write text, I added an EditText to my Activity with 0 width. 由于需要显示软键盘来编写文本,因此我在Activity中添加了一个EditText ,宽度为0。 I also implemented a TextWatcher to get the text entered into the EditText . 我还实现了TextWatcher来将文本输入到EditText With this trick, I can show the soft keyboard whenever I like with this code : 有了这个技巧,我可以随时通过以下代码显示软键盘:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(myEditText, InputMethodManager.SHOW_IMPLICIT);

Like this I'm able to know what the user is writing and to write the text inside my Canvas . 这样,我就可以知道用户在写什么,并且可以在Canvas写文本。

Now... this becomes tricky when the user would like to stop writing (or let say, anchor the text in the canvas definitely). 现在...当用户想要停止书写时(或者说可以肯定地将文本锚定在画布中),这变得很棘手。 What I thought is that he could press 'Enter'. 我以为他可以按Enter键。 So I tried to implement some way to catch the key events. 因此,我尝试实现某种方式来捕获关键事件。 Without any success so far. 到目前为止没有任何成功。

Here is my actual code. 这是我的实际代码。 This method is called when I would like to start writing. 当我想开始编写时,将调用此方法。 'edit' is an EditText . 'edit'是一个EditText

public void handleUp(final Paint myPaint) {
                edit.setFocusable(true);
                edit.setFocusableInTouchMode(true);
                edit.requestFocus();
                edit.addTextChangedListener(new Watcher());
                edit.setImeOptions(EditorInfo.IME_ACTION_GO);
                edit.setOnEditorActionListener(new OnEditorActionListener() {
                    public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                        Log.d("MyApp", "key pressed");
                        Paint localPaint = new Paint();
                        mCanvas.drawText(edit.getText().toString(), mX, mY, localPaint);
                        return false;
                    }
                });
                edit.setOnKeyListener(new OnKeyListener() {
                    public boolean onKey(View v, int keyCode, KeyEvent event) {

                        Log.d("MyApp", "key pressed");
                        if (keyCode == KeyEvent.ACTION_DOWN) {
                            Paint localPaint = new Paint();
                            mCanvas.drawText(edit.getText().toString(), mX, mY, localPaint);
                            return true;
                        }
                        return false;
                    }
                });
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);

            }

When I debug my app I never reach check points that I have put on my Log() and can't see any message in my Logs neither. 当我调试我的应用程序时,我从未到达我放在Log()上的检查点,并且在我的日志中也看不到任何消息。 I have seen many posts on StackOverFlow where this kind of implementation is used by I can't figure out why it fails here. 我在StackOverFlow上看到过很多帖子,其中使用了这种实现方式,我无法弄清楚为什么它会失败。

Thank you 谢谢

Taken from the documentation: 从文档中获取:

public void setOnKeyListener (View.OnKeyListener l)
Added in API level 1
Register a callback to be invoked when a hardware key is pressed in this view. Key presses    in software input methods will generally not trigger the methods of this listener.

So you should look for another listener. 因此,您应该寻找另一个听众。

My best guess would be to use this: 我最好的猜测是使用此:

public void setOnEditorActionListener (TextView.OnEditorActionListener l)
Added in API level 3
Set a special listener to be called when an action is performed on the text view. This   will be called when the enter key is pressed, or when an action supplied to the IME is selected by the user. Setting this means that the normal hard key event will not insert a newline into the text view, even if it is multi-line; holding down the ALT modifier will, however, allow the user to insert a newline character.

But none of the methods I could see in the documentation mentioned anything about soft key input. 但是我在文档中看不到的任何方法都涉及软键输入。

You override the dispatchKeyEvent to get the Enter key 您重写dispatchKeyEvent以获得Enter键

@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
    if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
    {
        // Do whatever you want
    }

    return super.dispatchKeyEvent(event);
}

Okay so finally the reason nothing worked was because my EditText had a width of 0. When I put width and height to 1. Setting visibility to View.INVISIBLE doesn't work in this case. 好的,最后,什么View.INVISIBLE不起作用的原因是因为我的EditText的宽度为0。当我将width和height设置为1时,在这种情况下无法设置View.INVISIBLE可见性。

By the way, the three Listener (OnEditorActionListener, OnKeyListener and Overriding dispatchKeyEvent) get the callbacks. 顺便说一句,三个侦听器(OnEditorActionListener,OnKeyListener和Overriding dispatchKeyEvent)获取回调。 But I'll use OnEditorActionListener because it's the only one getting only one callback. 但我将使用OnEditorActionListener因为它是唯一仅获得一个回调的OnEditorActionListener The two others get several callbacks which is problematic. 另外两个有几个回调,这是有问题的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM