Quellcode durchsuchen

[feat]键盘插件,选择键盘Activity,配置启动模式为singleInstance,单独分配一个Activity栈,避免在app中拉起InputMethodPicker时,会拉起自己的app

hezihao vor 7 Monaten
Ursprung
Commit
afe9c80b42

+ 1 - 1
plugins/keyboard_android/android/src/main/AndroidManifest.xml

@@ -14,7 +14,7 @@
 
         <activity
             android:name=".keyboard.InputMethodPickerActivity"
-            android:launchMode="singleTask"
+            android:launchMode="singleInstance"
             android:theme="@style/AppTheme.InputMethodPicker" />
 
         <service

+ 1 - 1
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/child/impl/AiKeyboardCommonPanelComponent.kt

@@ -223,7 +223,7 @@ class AiKeyboardCommonPanelComponent @JvmOverloads constructor(
         // 发送
         vSendBtn.click {
             KeyboardHolder.getKeyboardService()?.asInputMethodService()?.let {
-                InputMethodUtil.clickSendBtn(it.currentInputConnection)
+                InputMethodUtil.clickSendBtn(it)
             }
         }
     }

+ 1 - 1
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/child/impl/AiKeyboardProloguePanelComponent.kt

@@ -258,7 +258,7 @@ class AiKeyboardProloguePanelComponent @JvmOverloads constructor(
         // 发送
         vSendBtn.click {
             KeyboardHolder.getKeyboardService()?.asInputMethodService()?.let {
-                InputMethodUtil.clickSendBtn(it.currentInputConnection)
+                InputMethodUtil.clickSendBtn(it)
             }
         }
     }

+ 2 - 11
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/keyboard/InputMethodPickerActivity.kt

@@ -7,7 +7,7 @@ import android.content.Intent
 import android.os.Bundle
 import android.os.Handler
 import android.os.Looper
-import android.view.inputmethod.InputMethodManager
+import com.atmob.keyboard_android.util.InputMethodUtil
 import com.atmob.keyboard_android.util.LogUtil
 
 /**
@@ -54,7 +54,7 @@ class InputMethodPickerActivity : Activity() {
             //为了确保 Activity 完全进入前台后再弹出输入法选择器
             mMainHandler.postDelayed({
                 LogUtil.d("showInputMethodPicker triggered")
-                showInputMethodPicker()
+                InputMethodUtil.showInputMethodPicker(applicationContext)
             }, 100)
         } else {
             // 第二次获得焦点代表输入法弹窗消失
@@ -63,15 +63,6 @@ class InputMethodPickerActivity : Activity() {
         }
     }
 
-    /**
-     * 显示输入法选择器
-     */
-    private fun showInputMethodPicker() {
-        val inputMethodManager =
-            getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
-        inputMethodManager.showInputMethodPicker()
-    }
-
     override fun finish() {
         // 关闭时也不使用动画
         overridePendingTransition(0, 0)

+ 30 - 2
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/InputMethodUtil.kt

@@ -4,6 +4,8 @@ import android.content.ClipboardManager
 import android.content.Context
 import android.content.Context.CLIPBOARD_SERVICE
 import android.content.Intent
+import android.inputmethodservice.InputMethodService
+import android.os.Build
 import android.provider.Settings
 import android.view.inputmethod.EditorInfo
 import android.view.inputmethod.InputConnection
@@ -16,6 +18,15 @@ import android.widget.EditText
 class InputMethodUtil private constructor() {
     companion object {
         /**
+         * 显示输入法选择器
+         */
+        fun showInputMethodPicker(context: Context) {
+            val inputMethodManager =
+                context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
+            inputMethodManager.showInputMethodPicker()
+        }
+
+        /**
          * 打开输入法设置
          */
         fun openInputMethodSettings(context: Context) {
@@ -99,8 +110,25 @@ class InputMethodUtil private constructor() {
         /**
          * 自定义键盘,点击发送按钮,触发发送行为
          */
-        fun clickSendBtn(currentInputConnection: InputConnection) {
-            currentInputConnection.performEditorAction(EditorInfo.IME_ACTION_SEND)
+        fun clickSendBtn(inputMethodService: InputMethodService) {
+            inputMethodService.currentInputConnection.performEditorAction(EditorInfo.IME_ACTION_SEND)
+        }
+
+        /**
+         * 保持输入法显示
+         */
+        fun keepKeyboardShow(inputMethodService: InputMethodService) {
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
+                inputMethodService.requestShowSelf(0)
+            } else {
+                // 对于较低版本,尝试通过InputMethodManager重新显示键盘
+                val imm =
+                    inputMethodService.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
+                val view = inputMethodService.window.window?.decorView
+                view?.post {
+                    imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
+                }
+            }
         }
     }
 }