How to map accented characters in keyboard xml

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 08:56:53

问题


I have to implement a simple custom soft-keyboard in my application and I want to show accented characters on the keyboard too.

These are: í, é, á, ű, ú, ő, ó, ü, ö

My question is how to map these in the keyboard xml? What are the key codes for these? I could not found them in the official KeyEvent document.

My current keyboard definition xml looks like this:

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:horizontalGap="0px"
    android:verticalGap="0px"
    >

<Row android:keyHeight="16%">
    <Key android:codes="45" android:keyLabel="q" android:keyEdgeFlags="left"/>
    <Key android:codes="51" android:keyLabel="w"/>
    <Key android:codes="33" android:keyLabel="e"/>
    <Key android:codes="46" android:keyLabel="r"/>
    <Key android:codes="48" android:keyLabel="t"/>
    <Key android:codes="54" android:keyLabel="z"/>
    <Key android:codes="49" android:keyLabel="u"/>
    <Key android:codes="37" android:keyLabel="i"/>
    <Key android:codes="43" android:keyLabel="o"/>
    <Key android:codes="44" android:keyLabel="p" android:keyEdgeFlags="right"/>
</Row>

<Row android:keyHeight="16%">
    <Key android:codes="?" android:keyLabel="ö" android:keyEdgeFlags="left"/>
    <Key android:codes="?" android:keyLabel="ő"/>
    <Key android:codes="?" android:keyLabel="ü"/>
    <Key android:codes="?" android:keyLabel="ű"/>
    <Key android:codes="?" android:keyLabel="ó"/>
    <Key android:codes="?" android:keyLabel="é"/>
    <Key android:codes="?" android:keyLabel="á"/>
    <Key android:codes="?" android:keyLabel="í" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>

回答1:


Finally I solved it with a trick.

Inserted a custom keycode from 900 to 908 into the row which contains my accented characters.

<Row android:keyHeight="16%">
    <Key android:codes="900" android:keyLabel="ö" android:keyEdgeFlags="left"/>
    <Key android:codes="901" android:keyLabel="ő"/>
    <Key android:codes="902" android:keyLabel="ü"/>
    <Key android:codes="903" android:keyLabel="ű"/>
    <Key android:codes="904" android:keyLabel="ó"/>
    <Key android:codes="905" android:keyLabel="ú"/>
    <Key android:codes="906" android:keyLabel="á"/>
    <Key android:codes="907" android:keyLabel="é"/>
    <Key android:codes="908" android:keyLabel="í" android:keyEdgeFlags="right"/>
</Row>

Where i set my KeyboardView's Action Listener i am passing the target EditText as an object to my OnKeyboardActionListener.

This way in my onKey method of my OnKeyboardActionListener i can check if the primaryCode is greater than 900 or equals to it, so then i just simply set the text of the EditText and insert my accented character to the cursor position:

@Override
public void onKey(int primaryCode, int[] keyCodes) {        
    if(primaryCode >= 900) {
    String character = null;

    switch(keyCode) {
        case 900:
            character = "ö";
            break;
        case 901:
            character = "ő";
            break;
        case 902:
            character = "ü";
            break;
        case 903:
            character = "ű";
            break;
        case 904:
            character = "ó";
            break;
        case 905:
            character = "ú";
            break;
        case 906:
            character = "á";
            break;
        case 907:
            character = "é";
            break;
        case 908:
            character = "í";
            break;
        default:
            return;
    }

    int start = mTargetView.getSelectionStart();
    int end = mTargetView.getSelectionEnd();
    mTargetView.getText().replace(Math.min(start, end), Math.max(start, end), character, 0, character.length());
    } else {
        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);

        mTargetActivity.dispatchKeyEvent(event);
    }

}

Where mTargetView is my EditText. With this trick i could insert accented unicode characters and basically everything into an EditText with the standard keyboard xml mapping.




回答2:


A somewhat shorter solution is just to remove the codes because the label text gets sent in that case.

<Row>
    <Key android:keyLabel="ö" android:keyEdgeFlags="left"/>
    <Key android:keyLabel="ő"/>
    <Key android:keyLabel="ü"/>
    <Key android:keyLabel="ű"/>
    <Key android:keyLabel="ó"/>
    <Key android:keyLabel="ú"/>
    <Key android:keyLabel="á"/>
    <Key android:keyLabel="é"/>
    <Key android:keyLabel="í" android:keyEdgeFlags="right"/>
</Row>

See also How to make an Android custom keyboard.



来源:https://stackoverflow.com/questions/15417028/how-to-map-accented-characters-in-keyboard-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!