本文介绍了安卓:键盘捕行动进入按钮时pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以pressing一个标准的系统上的软屏连接时,键盘的数字模式输入键,作出回应。键盘出现时myCustomEditText类提供焦点,在屏幕上。

2试过的方法:


  1. 覆盖的onkeydown()


    公共布尔的onkeydown(INT键code,KeyEvent的事件){
        如果(键code == KeyEvent.KEY code_ENTER){
            //做某事..
            返回true;
        }
        返回super.onKeyDown(键code,事件);
    }


  1. 设置监听器 setOnKeyListener

    myCustomEditText.setOnKeyListener(新OnKeyListener(){        公共布尔安其(视图V,INT键code,KeyEvent的事件){
            如果(键code == KeyEvent.KEY code_ENTER){
                onRightButtonClicked();
                返回true;
            }
            返回false;
        }
    });

(也试过的onkeydown()在myCustomEditText为好,但没有效果)

的问题是:
这些aproaches以上全部为pressed按键作品,但对于输入键 - 方法,甚至没有被解雇的。 (这不是一个问题,不好 KeyEvent.KEY code _...

我的问题是:
为什么这些方法都没有被解雇的回车键,但对其他键,以及如何倾听回车键感pssed正确$ P $?


解决方案

您回应 ENTER 不同于普通钥匙,尝试是这样的:

  editText.setOnEditorActionListener(新TextView.OnEditorActionListener(){
    @覆盖
    公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){        开关(actionId){            案例EditorInfo.IME_ACTION_DONE:
                ...
                返回true;            默认:
                返回false;
        }
    }
});

您可以指定哪些动作是由执行 ENTER 按specifiying 的android:imeOptions XML或致电 EDITTEXT。 setImeOptions(...) EditorInfo 的一个常量。 imeOptions 也改变如何使用 ENTER 键长相!例如,如果您设置 imeOptions actionSearch 则 ENTER 键会看起来像一个搜索按钮。

您可以找到所有可能值 imeOptions 带有描述的。

I am trying to respond to pressing the enter button on a standard system on screen soft keybord, in numeric mode. keyboard appears on the screen when myCustomEditText class is given focus.

tried 2 approaches:

  1. overriding onKeyDown():


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            //do sth..
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

  1. setting listener setOnKeyListener:

    myCustomEditText.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                onRightButtonClicked();
                return true;
            }
            return false;
        }
    });

(also tried onKeyDown() in myCustomEditText as well, but with no effect)

problem is:these aproaches above works for all the keys being pressed but for the enter key - the methods are not even being fired at all. (it's not a problem of bad KeyEvent.KEYCODE_...)

my question is:why these methods are not being fired for enter key, but are for other keys, and how to listen to enter key being pressed properly?

解决方案

You respond to differently than normal keys, try something like this:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        switch (actionId) {

            case EditorInfo.IME_ACTION_DONE:
                ...
                return true;

            default:
                return false;
        }
    }
});

You can specify which action is performed by by specifiying android:imeOptions in XML or by calling editText.setImeOptions(...) with one of the constants in EditorInfo. imeOptions also changes how the key looks! For example if you set imeOptions to actionSearch then the key will look like a search button.

You can find a list of all possible values for imeOptions along with a description here.

这篇关于安卓:键盘捕行动进入按钮时pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 12:21