特定活动的自定义键盘

问题描述 投票:0回答:1

我已经使用InputMethodService()制作了一个自定义键盘,并将其注册在android studio的清单文件中,但问题是它使键盘成为全局的,这意味着现在当我打开键盘时,它只打开自定义键盘,但我的用例是我只想在单击某些编辑文本时打开我的自定义键盘,而对于其余的,在我的应用程序内只需要默认键盘。我怎样才能实现这个目标。

keys_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:horizontalGap="4dp"
    android:verticalGap="4dp"
    android:keyHeight="52dp"
    android:keyTextColor="#000000"
    android:keyBackground="@drawable/idk"
    >
    <Row>
        <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyLabel="2"/>
        <Key android:codes="51" android:keyLabel="3"/>
        <Key android:codes="52" android:keyLabel="4"/>
        <Key android:codes="53" android:keyLabel="5"/>
        <Key android:codes="54" android:keyLabel="6"/>
        <Key android:codes="55" android:keyLabel="7"/>
        <Key android:codes="56" android:keyLabel="8"/>
        <Key android:codes="57" android:keyLabel="9"/>
        <Key android:codes="48" android:keyLabel="0"/>
    </Row>
    <Row>
        <Key android:codes="-1" android:keyIcon="@drawable/ic_baseline_arrow_circle_up_24" android:keyEdgeFlags="left"/>
        <Key android:codes="122" android:keyLabel="z"/>
        <Key android:codes="120" android:keyLabel="x"/>
        <Key android:codes="99" android:keyLabel="c"/>
        <Key android:codes="118" android:keyLabel="v"/>
        <Key android:codes="98" android:keyLabel="b"/>
        <Key android:codes="110" android:keyLabel="n"/>
        <Key android:codes="109" android:keyLabel="m"/>
        <Key android:codes="-5" android:keyIcon="@drawable/ic_baseline_backspace_24" android:isRepeatable="true" android:keyEdgeFlags="right"/>
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key android:codes="44" android:keyLabel="123" android:keyWidth="25%p"  android:keyEdgeFlags="left"/>
        <Key android:codes="32" android:keyLabel="space" android:keyWidth="75%p" android:isRepeatable="true"/>
        <Key android:codes="-4" android:keyBackground="@color/colorPrimary" android:keyLabel="return" android:keyWidth="25%p" android:keyEdgeFlags="right"/>
    </Row>

</Keyboard>

method.xml
<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android">
    <subtype android:imeSubtypeMode="keyboard"
        android:imeSubtypeLocale="en_US"
        android:label="English (US)" />
</input-method>

keyboard_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/keyboard_view"
    android:keyPreviewLayout="@layout/key_preview"
    android:layout_alignParentBottom="true"
    android:background="@color/white"/>

key_preview
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@color/white"
    android:textColor="@color/black"
    android:textAlignment="center"
    android:textSize="30sp">
</TextView>




MyInputMethodService.kt

package com.example.myapplication

import android.inputmethodservice.InputMethodService
import android.inputmethodservice.Keyboard
import android.inputmethodservice.KeyboardView
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View

class MyInputMethodService : KeyboardView.OnKeyboardActionListener, InputMethodService() {
    private var keyboardView: KeyboardView? = null
    private var keyboard: Keyboard? = null
    private var caps = false

    override fun onPress(primaryCode: Int) {

    }

    override fun onRelease(primaryCode: Int) {

    }

    override fun onKey(primaryCode: Int, keyCodes: IntArray?) {
        val inputConnection = currentInputConnection
        if (inputConnection != null) {
            when (primaryCode) {
                Keyboard.KEYCODE_DELETE -> {
                    val selectedText = inputConnection.getSelectedText(0)
                    if (TextUtils.isEmpty(selectedText)) {
                        inputConnection.deleteSurroundingText(1, 0)
                    } else {
                        inputConnection.commitText("", 1)
                    }
                    caps = !caps
                    keyboard!!.isShifted = caps
                    keyboardView!!.invalidateAllKeys()
                }
                Keyboard.KEYCODE_SHIFT -> {
                    caps = !caps
                    keyboard!!.isShifted = caps
                    keyboardView!!.invalidateAllKeys()
                }
                Keyboard.KEYCODE_DONE -> inputConnection.sendKeyEvent(
                    KeyEvent(
                        KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_ENTER
                    )
                )
                else -> {
                    var code = primaryCode.toChar()
                    if (Character.isLetter(code) && caps) {
                        code = Character.toUpperCase(code)
                    }
                    inputConnection.commitText(code.toString(), 1)
                }
            }
        }
    }

    override fun onText(text: CharSequence?) {

    }

    override fun swipeLeft() {

    }

    override fun swipeRight() {

    }

    override fun swipeDown() {

    }

    override fun swipeUp() {

    }

    override fun onCreateInputView(): View {
        keyboardView = layoutInflater.inflate(R.layout.keyboard_view, null) as KeyboardView
        keyboard = Keyboard(this, R.xml.keys_layout)
        keyboardView!!.apply {
            keyboard = [email protected]
            setOnKeyboardActionListener(this@MyInputMethodService)
        }
        return keyboardView!!
    }
}
java android kotlin native custom-keyboard
1个回答
0
投票

您可以通过更改设备的输入法来实现。

检查下面的例子

import android.content.Context;
import android.view.inputmethod.InputMethodManager;

public class KeyboardUtils {

    public static void showCustomKeyboard(Context context) {
        // Get InputMethodManager
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        // Show your custom keyboard
        // Replace "yourCustomKeyboardView" with your actual custom keyboard view
        inputMethodManager.showInputMethodPicker();
    }

    public static void showDefaultKeyboard(Context context) {
        // Get InputMethodManager
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        // Hide your custom keyboard and show the default keyboard
        inputMethodManager.showInputMethodPicker();
    }
}

检查this代码和this以获取更多参考:

© www.soinside.com 2019 - 2024. All rights reserved.