如何在按下按钮时在特定应用中加载自定义键盘?

时间:2018-02-04 05:28:33

标签: android soft-keyboard

这是我在这里发表的第一篇文章。所以请忽略我的所有错误。我开发了一个Android应用程序。我需要一个自定义键盘,以便我可以使用本地语言。所以我在网上搜索后创建了一个自定义键盘。现在我想在从应用程序点击特定按钮时加载键盘,并在再次单击相同按钮时关闭并设置android的默认键盘。请任何人帮助我!

1.这就是说我想加载自定义键盘的write.class。

btnKeyboard.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    //                want to load the keyboard here!
    }
});

2.这来自Keyboard.class。

public class keyboardPE extends InputMethodService implements KeyboardView.OnKeyboardActionListener{
    private KeyboardView keyboardView;
    private Keyboard keyboard;

    Boolean caps = false;

    @Override
    public void onPress(int primaryCode) {

    }

    @Override
    public void onRelease(int primaryCode) {

    }

    @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;
                if(caps){
                    keyboard = new Keyboard(this, R.xml.qwerty_extra);
                    keyboardView.setKeyboard(keyboard);
                    keyboardView.setOnKeyboardActionListener(this);
                }
                else {
                    keyboard = new Keyboard(this, R.xml.qwerty);
                    keyboardView.setKeyboard(keyboard);
                    keyboardView.setOnKeyboardActionListener(this);
                }
                keyboard.setShifted(caps);
                keyboardView.invalidateAllKeys();
                break;
            case Keyboard.KEYCODE_DONE:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                break;
            default:
                char code = (char)primaryCode;
//                if(Character.isLetter(code) && caps){
//                    code = Character.toUpperCase(code);
//                }
                ic.commitText(String.valueOf(code),1);
        }
    }

    @Override
    public void onText(CharSequence text) {

    }

    @Override
    public void swipeLeft() {

    }

    @Override
    public void swipeRight() {

    }

    @Override
    public void swipeDown() {

    }

    @Override
    public void swipeUp() {

    }

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

    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);
        }
    }

}

3。 AndroidManifest文件。

    <activity
            android:name=".splash.SplashActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".keyboard.keyboardPE" android:label="@string/simple_ime"
            android:permission="android.permission.BIND_INPUT_METHOD">
            <meta-data android:name="android.view.im" android:resource="@xml/method"/>
            <inte`enter code here`nt-filter>
                <action android:name="android.view.InputMethod"/>
            </intent-filter>
        </service>

        <activity
            android:name=".Write"
            android:screenOrientation="portrait" />

0 个答案:

没有答案