当用户按下键盘上的按钮时在字母键盘和数字键盘之间切换

Switch between alphabetic keyboard and numeric keyboard when user presses a button on keyboard

我正在构建自定义 Hindi android soft keyboard 来自 this tutorial.I 在这个 [=16= 上有一个名为 123key ].我想将键盘布局更改为另一种由数字组成的布局(我已经为这个数字布局准备了 xml 文件)但我不知道。我应该怎么做? 这是我的 xml 印地文字母文件:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
        android:keyWidth="10%p"
        android:horizontalGap="0px"
        android:verticalGap="0px"
        android:keyHeight="60dp">
        <Row>
            <Key android:codes="0x0905" android:keyLabel="अ" android:keyEdgeFlags="left"/>
            <Key android:codes="0x0906" android:keyLabel="आ"/>
            <Key android:codes="0x0907" android:keyLabel="इ"/>
            <Key android:codes="0x0908" android:keyLabel="ई"/>
            <Key android:codes="0x0909" android:keyLabel="उ"/>
            <Key android:codes="0x090A" android:keyLabel="ऊ"/>
            <Key android:codes="0x090F" android:keyLabel="ए"/>
            <Key android:codes="0x0910" android:keyLabel="ऐ"/>
            <Key android:codes="0x0913" android:keyLabel="ओ"/>
            <Key android:codes="0x0914" android:keyLabel="औ" android:keyEdgeFlags="right"/>
        </Row>
        <Row>
            <Key android:codes="0x0915" android:keyLabel="क" android:keyEdgeFlags="left"/>
            <Key android:codes="0x0916" android:keyLabel="ख"/>
            <Key android:codes="0x0917" android:keyLabel="ग"/>
            <Key android:codes="0x0918" android:keyLabel="घ"/>
            <Key android:codes="0x091A" android:keyLabel="च"/>
            <Key android:codes="0x091B" android:keyLabel="छ"/>
            <Key android:codes="0x091C" android:keyLabel="ज"/>
            <Key android:codes="0x091D" android:keyLabel="झ"/>
            <Key android:codes="0x0902" android:keyLabel="ं"/>
            <Key android:codes="0x0903" android:keyLabel="ः" android:keyEdgeFlags="right"/>
        </Row>
        <Row>
            <Key android:codes="0x091F" android:keyLabel="ट" android:keyEdgeFlags="left"/>
            <Key android:codes="0x0920" android:keyLabel="ठ"/>
            <Key android:codes="0x0921" android:keyLabel="ड"/>
            <Key android:codes="0x0922" android:keyLabel="ढ"/>
            <Key android:codes="0x0924" android:keyLabel="त"/>
            <Key android:codes="0x0925" android:keyLabel="थ"/>
            <Key android:codes="0x0926" android:keyLabel="द"/>
            <Key android:codes="0x0927" android:keyLabel="ध"/>
            <Key android:codes="0x0928" android:keyLabel="न"/>
            <Key android:codes="-100" android:keyLabel="क्षत्रज्ञ" android:keyEdgeFlags="right"/>
        </Row>
        <Row>
            <Key android:codes="0x092A" android:keyLabel="प" android:keyEdgeFlags="left"/>
            <Key android:codes="0x092B" android:keyLabel="फ"/>
            <Key android:codes="0x092C" android:keyLabel="ब"/>
            <Key android:codes="0x092D" android:keyLabel="भ"/>
            <Key android:codes="0x092E" android:keyLabel="म"/>
            <Key android:codes="0x092F" android:keyLabel="य"/>
            <Key android:codes="0x0930" android:keyLabel="र"/>
            <Key android:codes="0x0932" android:keyLabel="ल"/>
            <Key android:codes="0x0935" android:keyLabel="व"/>
            <Key android:codes="-5" android:keyLabel="✖"
                android:isRepeatable="true"
                android:keyEdgeFlags="right" />
        </Row>
        <Row android:rowEdgeFlags="bottom">
            <Key android:codes="0" android:keyLabel="123"/>
            <Key android:codes="0x0936" android:keyLabel="श"/>
            <Key android:codes="0x0938" android:keyLabel="स"/>
            <Key android:codes="32" android:keyLabel="SPACE" android:keyWidth="40%p" android:isRepeatable="true"/>
            <Key android:codes="0x0939" android:keyLabel="ह"/>
            <Key android:codes="124"
                android:keyLabel="|"
                android:popupCharacters=",!.,?"
                android:popupKeyboard="@xml/commapopup"
                android:keyWidth="10%p"/>
            <Key android:codes="-4" android:keyLabel="DONE" android:keyEdgeFlags="right"/>
        </Row>
    </Keyboard>
</PreferenceScreen>

这是主要的 java 文件:

public class SimpleIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener {


    private KeyboardView kv;
    private Keyboard keyboard;

    private boolean caps = false;

    @Override
    public View onCreateInputView()
    {
        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.qwerty);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }

    private void playClick(int keyCode)
    {
        AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
        switch(keyCode)
        {
            case 32:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
                break;
            case Keyboard.KEYCODE_DONE:
            case 10:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
                break;
            case Keyboard.KEYCODE_DELETE:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
                break;
            default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
        }
    }


    @Override
    public void onKey(int primaryCode, int[] keyCodes)
    {
        InputConnection ic = getCurrentInputConnection();
        playClick(primaryCode);
        switch(primaryCode)
        {
            case Keyboard.KEYCODE_DELETE :
                ic.deleteSurroundingText(1, 0);
                break;
            case Keyboard.KEYCODE_SHIFT:
                caps = !caps;
                keyboard.setShifted(caps);
                kv.invalidateAllKeys();
                break;
            case Keyboard.KEYCODE_DONE:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                break;
            default:
                char code = (char)primaryCode;
//                Toast.makeText(getApplicationContext(),String.valueOf(code),Toast.LENGTH_SHORT).show();
                ic.commitText(String.valueOf(code),1);
        }
    }

    @Override
    public void onPress(int primaryCode) {
    }

    @Override
    public void onRelease(int primaryCode) {
    }

    @Override
    public void onText(CharSequence text) {
    }

    @Override
    public void swipeDown() {
    }

    @Override
    public void swipeLeft() {
    }

    @Override
    public void swipeRight() {
    }

    @Override
    public void swipeUp() {
    }
}

Onkey 方法的 switch 语句中将 case 设为 case 0(123 键的代码)并使用以下内容:

keyboard = new Keyboard(this, R.xml.number);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);

这里 number.xml 是数字的布局文件 keyboard.Hope 这很有帮助。