|
|
@@ -1,13 +1,9 @@
|
|
|
package com.atmob.keyboard_android.keyboard
|
|
|
|
|
|
-import android.annotation.SuppressLint
|
|
|
+import android.inputmethodservice.InputMethodService
|
|
|
import android.view.View
|
|
|
import android.view.Window
|
|
|
import android.view.inputmethod.EditorInfo
|
|
|
-import android.widget.Button
|
|
|
-import android.widget.EditText
|
|
|
-import android.widget.GridLayout
|
|
|
-import android.widget.Toast
|
|
|
import androidx.lifecycle.LifecycleOwner
|
|
|
import com.atmob.keyboard_android.R
|
|
|
import com.atmob.keyboard_android.keyboard.ext.InputMethodLifecycleService
|
|
|
@@ -62,7 +58,6 @@ class CustomKeyboardService : InputMethodLifecycleService(), ICustomKeyboardServ
|
|
|
if (flutterEngine != null) {
|
|
|
mMethodChannel =
|
|
|
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "keyboard_android")
|
|
|
- LogUtil.d("MethodChannel 初始化成功")
|
|
|
|
|
|
// 设置 MethodChannel 的回调
|
|
|
mMethodChannel.setMethodCallHandler { call: MethodCall, result: MethodChannel.Result ->
|
|
|
@@ -122,7 +117,6 @@ class CustomKeyboardService : InputMethodLifecycleService(), ICustomKeyboardServ
|
|
|
it["label"]!! to it["method"]!!
|
|
|
}
|
|
|
LogUtil.d("按键映射获取成功: $mKeyMappings")
|
|
|
- setupKeyboardButtons(vKeyboardView!!)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -137,61 +131,6 @@ class CustomKeyboardService : InputMethodLifecycleService(), ICustomKeyboardServ
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置键盘按钮
|
|
|
- *
|
|
|
- * @param keyboardView 键盘视图
|
|
|
- */
|
|
|
- @SuppressLint("ClickableViewAccessibility")
|
|
|
- private fun setupKeyboardButtons(keyboardView: View) {
|
|
|
- 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)
|
|
|
- val btnClear = keyboardView.findViewById<Button>(R.id.btn_clear)
|
|
|
- val btnSend = keyboardView.findViewById<Button>(R.id.btn_send)
|
|
|
- val numberPad = keyboardView.findViewById<GridLayout>(R.id.number_pad)
|
|
|
-
|
|
|
- // 禁止触摸输入
|
|
|
- editText.setOnTouchListener { _, _ ->
|
|
|
- true
|
|
|
- }
|
|
|
- // 清空,避免重复创建
|
|
|
- numberPad.removeAllViews()
|
|
|
-
|
|
|
- LogUtil.d("setupKeyboardButtons: 添加 ${mKeyMappings.size} 个按钮")
|
|
|
-
|
|
|
- mKeyMappings.forEach { (label, methodName) ->
|
|
|
- val button = Button(this).apply {
|
|
|
- text = label
|
|
|
- layoutParams = GridLayout.LayoutParams()
|
|
|
- // 每次点击时,都动态获取数据
|
|
|
- setOnClickListener {
|
|
|
- sendDynamicTextRequest(methodName, editText.text.toString())
|
|
|
- }
|
|
|
- }
|
|
|
- numberPad.addView(button)
|
|
|
- }
|
|
|
-
|
|
|
- btnPaste.setOnClickListener {
|
|
|
- InputMethodUtil.pasteInput(editText, fail = {
|
|
|
- Toast.makeText(this, "剪贴板为空", Toast.LENGTH_SHORT).show()
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- btnDelete.setOnClickListener {
|
|
|
- InputMethodUtil.deleteInput(currentInputConnection)
|
|
|
- }
|
|
|
-
|
|
|
- btnClear.setOnClickListener {
|
|
|
- InputMethodUtil.clearInput(currentInputConnection)
|
|
|
- }
|
|
|
-
|
|
|
- btnSend.setOnClickListener {
|
|
|
- InputMethodUtil.clickSendBtn(currentInputConnection)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* 动态调用 Flutter 获取文本
|
|
|
*
|
|
|
* @param methodName 方法名
|
|
|
@@ -227,4 +166,8 @@ class CustomKeyboardService : InputMethodLifecycleService(), ICustomKeyboardServ
|
|
|
override fun getLifecycleOwner(): LifecycleOwner {
|
|
|
return this
|
|
|
}
|
|
|
+
|
|
|
+ override fun asInputMethodService(): InputMethodService {
|
|
|
+ return this
|
|
|
+ }
|
|
|
}
|