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