Sfoglia il codice sorgente

feat:键盘插件,优化删除按钮,实现长按时,可以持续删除文本

hezihao 8 mesi fa
parent
commit
6897a94c91

+ 4 - 2
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/child/impl/AiKeyboardComponent.kt

@@ -19,6 +19,7 @@ import com.atmob.keyboard_android.model.AiKeyboardKeyModel
 import com.atmob.keyboard_android.util.InputMethodUtil
 import com.atmob.keyboard_android.util.KeyboardHolder
 import com.atmob.keyboard_android.util.recyclerview.GridDivider
+import com.atmob.keyboard_android.widget.LongTouchContainer
 import com.atmob.keyboard_android.widget.indicator.TabPagerTitleView
 import com.blankj.utilcode.util.ConvertUtils
 import me.drakeet.multitype.Items
@@ -42,7 +43,7 @@ class AiKeyboardComponent @JvmOverloads constructor(
     private lateinit var vPasteBarLayout: View
     private lateinit var vKeyList: RecyclerView
     private lateinit var vPasteBtn: View
-    private lateinit var vDeleteBtn: View
+    private lateinit var vDeleteBtn: LongTouchContainer
     private lateinit var vClearBtn: View
     private lateinit var vSendBtn: View
 
@@ -181,6 +182,7 @@ class AiKeyboardComponent @JvmOverloads constructor(
     /**
      * 设置操作按钮
      */
+    @SuppressLint("ClickableViewAccessibility")
     private fun setupActionBtn() {
         // 粘贴
         vPasteBtn.click {
@@ -195,7 +197,7 @@ class AiKeyboardComponent @JvmOverloads constructor(
         }
 
         // 删除
-        vDeleteBtn.click {
+        vDeleteBtn.setOnLongPressListener {
             KeyboardHolder.getKeyboardService()?.asInputMethodService()?.let {
                 InputMethodUtil.deleteInput(it.currentInputConnection)
             }

+ 88 - 0
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/widget/LongTouchContainer.java

@@ -0,0 +1,88 @@
+package com.atmob.keyboard_android.widget;
+
+import android.content.Context;
+import android.os.Handler;
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+import android.widget.FrameLayout;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+/**
+ * 长按检测容器
+ */
+public class LongTouchContainer extends FrameLayout {
+    /**
+     * 长按触发阈值,单位为毫秒
+     */
+    final int LONG_PRESS_DELAY = 100;
+
+    private final Handler mMainHandler = new Handler();
+
+    /**
+     * 长按回调
+     */
+    private OnLongPressListener mOnLongPressListener;
+
+    public LongTouchContainer(@NonNull Context context) {
+        super(context);
+    }
+
+    public LongTouchContainer(@NonNull Context context, @Nullable AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public LongTouchContainer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+    }
+
+    public LongTouchContainer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
+        super(context, attrs, defStyleAttr, defStyleRes);
+    }
+
+    @Override
+    protected void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        mMainHandler.removeCallbacksAndMessages(null);
+    }
+
+    @Override
+    public boolean onTouchEvent(MotionEvent event) {
+        switch (event.getAction()) {
+            case MotionEvent.ACTION_DOWN:
+                // 按下时,开始长按检测
+                mMainHandler.postDelayed(new Runnable() {
+                    @Override
+                    public void run() {
+                        // 长按,持续回调
+                        if (mOnLongPressListener != null) {
+                            mOnLongPressListener.onLongPress();
+                        }
+                        mMainHandler.postDelayed(this, LONG_PRESS_DELAY);
+                    }
+                }, LONG_PRESS_DELAY);
+                break;
+            case MotionEvent.ACTION_UP:
+                // 松开时移除回调
+                mMainHandler.removeCallbacksAndMessages(null);
+                // 松手那一刻,也回调一次
+                if (mOnLongPressListener != null) {
+                    mOnLongPressListener.onLongPress();
+                }
+                break;
+        }
+        return true; // 消费事件,避免点击冲突
+    }
+
+    public interface OnLongPressListener {
+        void onLongPress();
+    }
+
+    /**
+     * 设置长按回调
+     */
+    public void setOnLongPressListener(OnLongPressListener listener) {
+        this.mOnLongPressListener = listener;
+    }
+}

+ 12 - 4
plugins/keyboard_android/android/src/main/res/layout/component_ai_keyboard.xml

@@ -4,7 +4,8 @@
     android:layout_width="match_parent"
     android:layout_height="@dimen/keyboard_content_height"
     android:orientation="vertical"
-    tools:background="@mipmap/bg_keyboard">
+    tools:background="@mipmap/bg_keyboard"
+    tools:layout_height="wrap_content">
 
     <com.atmob.keyboard_android.component.child.impl.PermissionTipComponent
         android:id="@+id/permission_tip"
@@ -101,10 +102,17 @@
                 android:layout_marginEnd="10dp"
                 android:orientation="vertical">
 
-                <TextView
+                <com.atmob.keyboard_android.widget.LongTouchContainer
                     android:id="@+id/delete_btn"
-                    style="@style/keyboard_action_btn"
-                    android:text="@string/delete" />
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_marginBottom="6dp">
+
+                    <TextView
+                        style="@style/keyboard_action_btn"
+                        android:layout_marginBottom="0dp"
+                        android:text="@string/delete" />
+                </com.atmob.keyboard_android.widget.LongTouchContainer>
 
                 <TextView
                     android:id="@+id/clear_btn"