Bladeren bron

[feat]键盘插件,抽取代码,封装工具类

hezihao 8 maanden geleden
bovenliggende
commit
6ba6ec494a

+ 1 - 0
plugins/keyboard_android/android/build.gradle

@@ -64,6 +64,7 @@ android {
         testImplementation("org.jetbrains.kotlin:kotlin-test")
         testImplementation("org.mockito:mockito-core:5.0.0")
         compileOnly(files("$flutterSdk/bin/cache/artifacts/engine/android-arm/flutter.jar"))
+        implementation 'androidx.recyclerview:recyclerview:1.3.1'
     }
 
     testOptions {

+ 1 - 10
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/floating/FloatingButtonService.kt

@@ -17,7 +17,6 @@ import android.view.WindowManager
 import android.widget.ImageView
 import com.atmob.keyboard_android.R
 import com.atmob.keyboard_android.keyboard.InputMethodPickerActivity
-import com.atmob.keyboard_android.util.LogUtil
 import kotlin.math.abs
 
 /**
@@ -59,7 +58,6 @@ class FloatingButtonService : Service() {
     @SuppressLint("ClickableViewAccessibility")
     override fun onCreate() {
         super.onCreate()
-        LogUtil.d("onCreate: ")
         // 初始化 WindowManager
         windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
 
@@ -139,7 +137,7 @@ class FloatingButtonService : Service() {
 
         // 点击按钮,弹出输入法选择框
         floatingButton.setOnClickListener {
-            showInputMethodPicker()
+            InputMethodPickerActivity.start(this)
         }
 
         // 添加悬浮窗到屏幕
@@ -147,13 +145,6 @@ class FloatingButtonService : Service() {
     }
 
     /**
-     * 显示输入法选择框
-     */
-    private fun showInputMethodPicker() {
-        InputMethodPickerActivity.start(this)
-    }
-
-    /**
      * 吸附到最近的屏幕边缘
      */
     private fun snapToEdge() {

+ 20 - 33
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/keyboard/CustomKeyboardService.kt

@@ -1,7 +1,6 @@
 package com.atmob.keyboard_android.keyboard
 
 import android.annotation.SuppressLint
-import android.content.ClipboardManager
 import android.inputmethodservice.InputMethodService
 import android.view.View
 import android.view.inputmethod.EditorInfo
@@ -10,6 +9,7 @@ import android.widget.EditText
 import android.widget.GridLayout
 import android.widget.Toast
 import com.atmob.keyboard_android.R
+import com.atmob.keyboard_android.util.InputMethodUtil
 import com.atmob.keyboard_android.util.LogUtil
 import io.flutter.embedding.engine.FlutterEngineCache
 import io.flutter.plugin.common.MethodCall
@@ -50,7 +50,7 @@ class CustomKeyboardService : InputMethodService() {
                     "inputText" -> {
                         val text = call.argument<String>("text")
                         if (text != null) {
-                            inputText(text)
+                            InputMethodUtil.inputText(this.currentInputConnection, text)
                         }
                     }
 
@@ -82,7 +82,9 @@ class CustomKeyboardService : InputMethodService() {
         fetchKeyMappings()
     }
 
-    /**通过 KeyboardAndroidPlugin 获取按键映射**/
+    /**
+     * 通过 KeyboardAndroidPlugin 获取按键映射
+     */
     private fun fetchKeyMappings() {
         // 通过 methodChannel 获取按键映射
         mMethodChannel.invokeMethod("getKeyMappings", null, object : MethodChannel.Result {
@@ -107,7 +109,6 @@ class CustomKeyboardService : InputMethodService() {
         })
     }
 
-
     /**
      * 设置键盘按钮
      *
@@ -115,7 +116,7 @@ class CustomKeyboardService : InputMethodService() {
      */
     @SuppressLint("ClickableViewAccessibility")
     private fun setupKeyboardButtons(keyboardView: View) {
-        LogUtil.d("当前键映射: $mKeyMappings") // 打印映射
+        LogUtil.d("当前键映射: $mKeyMappings")
         val editText = keyboardView.findViewById<EditText>(R.id.keyboard_input)
         val btnPaste = keyboardView.findViewById<Button>(R.id.btn_paste)
         val btnDelete = keyboardView.findViewById<Button>(R.id.btn_delete)
@@ -124,7 +125,9 @@ class CustomKeyboardService : InputMethodService() {
         val numberPad = keyboardView.findViewById<GridLayout>(R.id.number_pad)
 
         // 禁止触摸输入
-        editText.setOnTouchListener { _, _ -> true }
+        editText.setOnTouchListener { _, _ ->
+            true
+        }
         // 清空,避免重复创建
         numberPad.removeAllViews()
 
@@ -134,8 +137,8 @@ class CustomKeyboardService : InputMethodService() {
             val button = Button(this).apply {
                 text = label
                 layoutParams = GridLayout.LayoutParams()
+                // 每次点击时,都动态获取数据
                 setOnClickListener {
-                    // 每次点击时动态获取数据
                     sendDynamicTextRequest(methodName, editText.text.toString())
                 }
             }
@@ -143,21 +146,23 @@ class CustomKeyboardService : InputMethodService() {
         }
 
         btnPaste.setOnClickListener {
-            pasteText(editText)
+            InputMethodUtil.pasteInput(editText, fail = {
+                Toast.makeText(this, "剪贴板为空", Toast.LENGTH_SHORT).show()
+            })
         }
 
         btnDelete.setOnClickListener {
-            currentInputConnection.deleteSurroundingText(1, 0)
+            InputMethodUtil.deleteInput(currentInputConnection)
         }
+
         btnClear.setOnClickListener {
-            currentInputConnection.deleteSurroundingText(
-                Int.MAX_VALUE,
-                Int.MAX_VALUE
-            )
+            InputMethodUtil.clearInput(currentInputConnection)
         }
-        btnSend.setOnClickListener { currentInputConnection.performEditorAction(android.view.inputmethod.EditorInfo.IME_ACTION_SEND) }
-    }
 
+        btnSend.setOnClickListener {
+            InputMethodUtil.clickSendBtn(currentInputConnection)
+        }
+    }
 
     /**
      * 动态调用 Flutter 获取文本
@@ -183,22 +188,4 @@ class CustomKeyboardService : InputMethodService() {
                 }
             })
     }
-
-    /**
-     * 剪贴板粘贴
-     */
-    private fun pasteText(editText: EditText) {
-        val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
-        val clipData = clipboard.primaryClip
-        if (clipData != null && clipData.itemCount > 0) {
-            editText.setText(clipData.getItemAt(0).text.toString())
-        } else {
-            Toast.makeText(this, "剪贴板为空", Toast.LENGTH_SHORT).show()
-        }
-    }
-
-    private fun inputText(text: String) {
-        LogUtil.d("inputText: $text")
-        currentInputConnection.commitText(text, 1)
-    }
 }

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

@@ -11,7 +11,7 @@ import android.view.inputmethod.InputMethodManager
 import com.atmob.keyboard_android.util.LogUtil
 
 /**
- * 选择输入法的页面
+ * 选择输入法的页面,是一个透明的Activity,用于触发系统的输入法选择器,选择完就自动关闭
  */
 class InputMethodPickerActivity : Activity() {
     private var isFirst = true

+ 54 - 0
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/InputMethodUtil.kt

@@ -1,9 +1,14 @@
 package com.atmob.keyboard_android.util
 
+import android.content.ClipboardManager
 import android.content.Context
+import android.content.Context.CLIPBOARD_SERVICE
 import android.content.Intent
 import android.provider.Settings
+import android.view.inputmethod.EditorInfo
+import android.view.inputmethod.InputConnection
 import android.view.inputmethod.InputMethodManager
+import android.widget.EditText
 
 /**
  * 输入法工具类
@@ -27,5 +32,54 @@ class InputMethodUtil private constructor() {
             val enabledInputMethods = imm.enabledInputMethodList
             return enabledInputMethods.any { it.packageName == context.packageName }
         }
+
+        /**
+         * 粘贴文本到输入框中
+         */
+        fun pasteInput(
+            editText: EditText,
+            success: (() -> Unit)? = null,
+            fail: (() -> Unit)? = null
+        ) {
+            val clipboard = editText.context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
+            val clipData = clipboard.primaryClip
+            if (clipData != null && clipData.itemCount > 0) {
+                editText.setText(clipData.getItemAt(0).text.toString())
+                success?.invoke()
+            } else {
+                fail?.invoke()
+            }
+        }
+
+        /**
+         * 自定义键盘,点击输入法的按键,输入指定的文本
+         */
+        fun inputText(currentInputConnection: InputConnection, text: String) {
+            currentInputConnection.commitText(text, 1)
+        }
+
+        /**
+         * 自定义键盘,点击删除按钮,删除文本
+         */
+        fun deleteInput(currentInputConnection: InputConnection) {
+            currentInputConnection.deleteSurroundingText(1, 0)
+        }
+
+        /**
+         * 自定义键盘,点击清空按钮,清空所有文本
+         */
+        fun clearInput(currentInputConnection: InputConnection) {
+            currentInputConnection.deleteSurroundingText(
+                Int.MAX_VALUE,
+                Int.MAX_VALUE
+            )
+        }
+
+        /**
+         * 自定义键盘,点击发送按钮,触发发送行为
+         */
+        fun clickSendBtn(currentInputConnection: InputConnection) {
+            currentInputConnection.performEditorAction(EditorInfo.IME_ACTION_SEND)
+        }
     }
 }