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

时间:2013-04-02 20:18:58

标签: android android-canvas android-edittext android-softkeyboard

我正在尝试在Canvas中撰写文字。由于我需要显示软键盘来写文本,我在{0}宽度的活动中添加了EditText。我还实现了一个TextWatcher来获取输入EditText的文本。有了这个技巧,我可以随时使用这段代码显示软键盘:

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

像这样,我能够知道用户在写什么,并在Canvas内写下文字。

现在......当用户想要停止写作时(或者说,明确地将文本锚定在画布中),这变得棘手。我认为他可以按'Enter'。所以我试着用一些方法来捕捉关键事件。到目前为止没有任何成功。

这是我的实际代码。当我想开始写作时调用此方法。 '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);

            }

当我调试我的应用时,我从未到达我放在Log()上的检查点,也无法在我的日志中看到任何消息。我在StackOverFlow上看过很多帖子,我使用这种实现方法,我无法弄清楚它为什么会失败。

谢谢

3 个答案:

答案 0 :(得分:0)

取自文件:

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.

所以你应该寻找另一个听众。

我最好的猜测是使用它:

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.

但是我在文档中看不到任何关于软键输入的方法。

答案 1 :(得分:0)

您覆盖dispatchKeyEvent以获取Enter键

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

    return super.dispatchKeyEvent(event);
}

答案 2 :(得分:-1)

好的,最后无效的原因是因为我的EditText宽度为0.当我将宽度和高度设置为1.设置View.INVISIBLE的可见性在这种情况下不起作用。

顺便说一句,三个Listener(OnEditorActionListener,OnKeyListener和Overriding dispatchKeyEvent)获取回调。但是我会使用OnEditorActionListener,因为它是唯一只获得一次回调的人。另外两个人得到了几个有问题的回调。