OnKeyboardActionListener - event.getUnicodeChar总是返回"' \ u0000' 0"

时间:2017-11-16 14:38:49

标签: java android xml keyboard-events

我有一个使用以下xml的KeyboardView

    <?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:horizontalGap="0px"
    android:verticalGap="0px"
    android:keyHeight="32dp"
    >

    <Row>
        <Key android:codes="113" android:keyLabel="q" android:keyEdgeFlags="left"/>
        <Key android:codes="119" android:keyLabel="w"/>
        <Key android:codes="101" android:keyLabel="e"/>
        <Key android:codes="114" android:keyLabel="r"/>
        <Key android:codes="116" android:keyLabel="t"/>
        <Key android:codes="121" android:keyLabel="y"/>
        <Key android:codes="117" android:keyLabel="u"/>
        <Key android:codes="105" android:keyLabel="i"/>
        <Key android:codes="111" android:keyLabel="o"/>
        <Key android:codes="112" android:keyLabel="p" android:keyEdgeFlags="right"/>
    </Row>

    <Row>
        <Key android:codes="97" android:keyLabel="a" android:horizontalGap="5%p"
            android:keyEdgeFlags="left"/>
        <Key android:codes="115" android:keyLabel="s"/>
        <Key android:codes="100" android:keyLabel="d"/>
        <Key android:codes="102" android:keyLabel="f"/>
        <Key android:codes="103" android:keyLabel="g"/>
        <Key android:codes="104" android:keyLabel="h"/>
        <Key android:codes="106" android:keyLabel="j"/>
        <Key android:codes="107" android:keyLabel="k"/>
        <Key android:codes="108" android:keyLabel="l" android:keyEdgeFlags="right"/>
    </Row>

    <Row>
        <Key android:codes="-1" android:keyLabel="^"
            android:keyWidth="15%p" android:isModifier="true"
            android:isSticky="true" android:keyEdgeFlags="left"/>
        <Key android:codes="122" android:keyLabel="z"/>
        <Key android:codes="120" android:keyLabel="x"/>
        <Key android:codes="99" android:keyLabel="c"/>
        <Key android:codes="118" android:keyLabel="v"/>
        <Key android:codes="98" android:keyLabel="b"/>
        <Key android:codes="110" android:keyLabel="n"/>
        <Key android:codes="109" android:keyLabel="m"/>
        <Key android:codes="-5" android:keyLabel="zz"
            android:keyWidth="15%p" android:keyEdgeFlags="right"/>
    </Row>

    <Row android:rowEdgeFlags="bottom">
        <Key android:codes="-2" android:keyLabel="123" android:keyWidth="15%p"/>
        <Key android:codes="32" android:keyLabel=" "
            android:keyWidth="50%p" android:isRepeatable="true"/>
        <Key android:codes="46,44" android:keyLabel=". ,"
            android:keyWidth="15%p"/>
        <Key android:codes="10" android:keyLabel="ent"
            android:keyWidth="20%p" android:keyEdgeFlags="right"/>
    </Row>
</Keyboard>

在我的java中,我有这个代码来管理当我按下一个键时发生的事情

qerty_keyboard.setPreviewEnabled(false);
    keyboard = new Keyboard(context, R.xml.keyboard_querty);
    qerty_keyboard.setKeyboard(keyboard);
    qerty_keyboard.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int keyCode) {

        }

        @Override
        public void onRelease(int primaryCode) {
            long eventTime = System.currentTimeMillis();
            KeyEvent event = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, primaryCode, 0, 0, 0, 0, KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE);
            switch (primaryCode){
                /* .. */
                default:
                    char unicodeChar = (char) event.getUnicodeChar();
                    Log.e("KEY","CODE "+primaryCode + " "+unicodeChar);
                    et_search.getText().append(unicodeChar);
                    break;
            }
        }
        @Override
        public void onKey(int keyCode, int[] ints) {

        }

        @Override
        public void onText(CharSequence charSequence) {

        }

        @Override
        public void swipeLeft() {

        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeDown() {

        }

        @Override
        public void swipeUp() {

        }
    });
}

我的问题是我从getUnicodeChar()获得的字符始终是'\u0000' 0,除非在这些情况下:

E/KEY: CODE 32 d
E/KEY: CODE 46 r
E/KEY: CODE 10 3

我有一个相同的代码,数字键盘运行良好,但使用此.xml似乎会导致某种问题。 请指教。

0 个答案:

没有答案