本文介绍了安卓SoftKeyboard的onkeydown /上未检测出“另类”键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有处理输入我的一个观点,我弹出一个键盘设置视图可获得焦点。现在,我可以得到一些关键presses ...

I have a view which handles input for me, I pop up a keyboard and set the view focusable. Now I can get certain key presses...

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_DEL) {
    } else if (keyCode == KeyEvent.KEYCODE_BACK) {
    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
    } else {
    }
}

等等...性格pressed我得到使用

and so on... the character pressed I get by using

event.getDisplayLabel()

只要我只想要正常的字母AZ工程。在其它语言中,更多的字母可通过长pressing软键盘上的正常信达成......然而,这些替代的信不能被的onkeydown /向上检测到。我只能检测到正常的字母,软键盘的标签。现在,我的应用程序必须处理外国输入字母时,我已在键盘改为土耳其,我可以找到像I I U U字母在键盘上,但如果我preSS他们,我没有得到任何回应。不与event.getDisplayLabel也不event.getUni codeCHAR();我如何检测这些信吗?

That works as long as I only want the normal letters A-Z.In other languages, more letters can be reached by long pressing a normal letter on the soft keyboard... however, these alternative letters cannot be detected by onKeyDown/Up. I can only detect the normal letters, the labels of the soft keyboard. Now my app has to process foreign input and letters, I have changed the keyboard to turkish and I can find letters like í ì ú ù on the keyboard, but if I press them, I don't get any response. Not with event.getDisplayLabel nor event.getUnicodeChar();How do I detect these letters?

推荐答案

当键盘打开时,的onkeydown()的onkeyup()方法将无法正常工作。

When the keyboard is open, onKeyDown() and onKeyUp() methods don't work properly because Android considers on-screen keyboard as a separate activity.

最简单的方法来实现你想要的是重写 onKey $ P $宗座外方传教会()方法对你的看法。例如,如果你想从一个EditText捕获的onkeydown,创建一个新的类延伸的EditText ,并覆盖 onKey $ P $宗座外方传教会()方法:

The easiest way to achieve what you want is to override onKeyPreIme() method on your view. For example, if you're trying to capture onKeyDown from an EditText, create a new Class which extends EditText, and override the onKeyPreIme() method:

public class LoseFocusEditText extends EditText {

    private Context mContext;

    protected final String TAG = getClass().getName();

    public LoseFocusEditText(Context context) {
        super(context);
        mContext = context;
    }

    public LoseFocusEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public LoseFocusEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            //hide keyboard
            InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);

            //lose focus
            this.clearFocus();

            return true;
        }
        return false;
    }
}

这是对奇巧/的HTC One测试。

This was tested on kitkat / htc one.

这篇关于安卓SoftKeyboard的onkeydown /上未检测出“另类”键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:44