How to map accented characters in keyboard xml

后端 未结 2 1102
眼角桃花
眼角桃花 2020-12-21 08:35

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: í, é, á, ű, ú, ő, ó, ü, ö<

相关标签:
2条回答
  • 2020-12-21 09:17

    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.

    0 讨论(0)
  • 2020-12-21 09:23

    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.

    0 讨论(0)
提交回复
热议问题