如何覆盖Android中虚拟键盘的<ENTER>键行为

Aak*_*Aak 7 keyboard virtual android

我想覆盖ENTER虚拟键盘的键的行为,以便:

  • 当屏幕上有更多字段时,它会"标记"到下一个字段
  • 当它是屏幕的最后一个字段时,它执行屏幕的默认操作

我一直在玩IME选项和标签,但只是没有得到我想要的.有人有什么建议吗?

Aak*_*Aak 9

在另一个论坛的帮助下,我找到了做到这一点的方法.

为了使它可重用,我创建了自己的超级对话框类,它包含2个OnKeyListener对象和一个抽象的提交方法:

public abstract class MyAbstractDialog extends Dialog {

/**
 * OnKeyListener that puts the focus down when the ENTER key is pressed
 */
protected View.OnKeyListener onEnterFocusDown = new View.OnKeyListener() {

                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                            (keyCode == KeyEvent.KEYCODE_ENTER)) {
                                v.requestFocus(View.FOCUS_DOWN);
                        return true;
                }
                        return false;
                }
        };

/**
 * OnKeyListener that submits the page when the ENTER key is pressed
 */
protected View.OnKeyListener onEnterSubmitView = new View.OnKeyListener() {

                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                            (keyCode == KeyEvent.KEYCODE_ENTER)) {
                                submitView(v);
                        return true;
                }
                        return false;
                }
        };
        protected abstract void submitView(View v);

}
Run Code Online (Sandbox Code Playgroud)

现在在Dialog我可以使用这些对象在我的字段上设置:

// make the ENTER key on passwordField1 put the focus on the next field
passwordField1.setOnKeyListener(onEnterFocusDown);

// make the ENTER key on passwordField2 submit the page
passwordField2.setOnKeyListener(onEnterSubmitView);
Run Code Online (Sandbox Code Playgroud)