Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

android - How to map accented characters in keyboard xml

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...