即使在LongPress上触发了MotionEvent操作MOVE之后,按键仍保持按下状态

问题描述

嗨,我正在使用Keyboard应用程序,并且使用了AOSP中经过稍微修改的KeyboardView类。当长按一个键时,我已重写onLongPress方法以在弹出窗口上显示迷你键盘(带重音符号的字符)。一切正常,但即使显示了迷你键盘,长按的键仍保持按下状态。显示键盘窗口后,如何恢复到正常的未按下状态?

以下是我在长按中显示迷你键盘的操作:

  @Override
    public boolean onLongPress(AppKeyboard.Key popupKey) {

        if (this.popupWindow != null)
            return false;

        if (popupKey.codes[0] == 1) {
            ContextThemeWrapper ctx = new ContextThemeWrapper(context,R.style.AppTheme);
            LayoutInflater inflater = LayoutInflater.from(ctx);

            View popupView = inflater.inflate(R.layout.popup_layout,null);
            FontsKeyboardView keyboardView = popupView.findViewById(R.id.popup_keyboard_view);
            keyboardView.setClipToOutline(true);
            AppKeyboard keyboard = new AppKeyboard(context,R.xml.popup_test);
            keyboardView.setKeyboard(keyboard);
            popupWindow = new PopupWindow(ctx);
            //popupWindow.setBackgroundDrawable(null);
            popupWindow.setContentView(popupView);
            popupWindow.setAttachedindecor(false);
            popupWindow.setoutsidetouchable(true);

            popupWindow.getContentView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    View contentView = popupWindow.getContentView();

                    contentView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    int[] iArr = new int[2];

                    popupWindow.getContentView().getLocationOnScreen(iArr);

                    int[] r0 = new int[2];

                    getLocationOnScreen(r0);

                    windowPopupOffset[0] = (iArr[0] - r0[0]) + windowOffset[0];
                    windowPopupOffset[1] = (iArr[1] - r0[1]) + windowOffset[1];

                }
            });

            getLocationInWindow(this.windowOffset);
            int i = this.windowOffset[0] + popupKey.x;
            popupView.measure(MeasureSpec.makeMeasureSpec(getWidth(),MeasureSpec.AT_MOST),MeasureSpec.makeMeasureSpec(getHeight(),MeasureSpec.AT_MOST));
            measuredHeight = popupView.getMeasuredHeight();
            int heightoffset = (this.windowOffset[1] + popupKey.y) - measuredHeight;
            //Toast.makeText(context,String.valueOf(measuredHeight),Toast.LENGTH_SHORT).show();
            this.popupWindow = popupWindow;
            popupWindow.showAtLocation(this,i,heightoffset);

            invalidateallKeys();
            return true;
        }

        return super.onLongPress(popupKey);

    }

这是键盘视图的onTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent motionEvent) {

    if (this.popupWindow != null) {
        //  Toast.makeText(context,"Executed",Toast.LENGTH_SHORT).show();
        PopupWindow popupWindow;
        if (motionEvent.getAction() == MotionEvent.ACTION_POINTER_UP || motionEvent.getAction() == MotionEvent.ACTION_UP) {
            motionEvent = translatetoPopupCoordinates(motionEvent,MotionEvent.ACTION_UP);
            popupWindow = this.popupWindow;
            popupWindow.getContentView().onTouchEvent(motionEvent);
            motionEvent.recycle();
            PopupWindow popupWindow2 = this.popupWindow;
            popupWindow2.dismiss();
            this.popupWindow = (PopupWindow) null;
            return true;

        } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {

            popupWindow = this.popupWindow;
            View contentView = popupWindow.getContentView();
            if (!contentView.isAttachedToWindow()) {
                return true;
            }
            motionEvent = translatetoPopupCoordinates(motionEvent,MotionEvent.ACTION_DOWN);
            popupWindow = this.popupWindow;
            popupWindow.getContentView().onTouchEvent(motionEvent);
            motionEvent.recycle();

            return true;
        }
        else if (motionEvent.getAction()==MotionEvent.ACTION_POINTER_DOWN || motionEvent.getAction()==MotionEvent.ACTION_DOWN){
            motionEvent = translatetoPopupCoordinates(motionEvent,MotionEvent.ACTION_DOWN);
            motionEvent.recycle();

        }
    }
    return super.onTouchEvent(motionEvent);
}

然后将motionevent从键盘注入到弹出窗口键盘

@SuppressLint("Recycle")
private final MotionEvent translatetoPopupCoordinates(MotionEvent motionEvent,int action) {
    long downTime = motionEvent.getDownTime();
    long eventTime = motionEvent.getEventTime();
    float x = ((float) this.windowOffset[0]) + (motionEvent.getX() - ((float) this.windowPopupOffset[0]));
    float y = (motionEvent.getY() - ((float) this.windowPopupOffset[1])) + ((float) this.windowOffset[1]);
    PopupWindow popupWindow = this.popupWindow;
    View contentView = popupWindow.getContentView();


    MotionEvent currMotionEvent = MotionEvent.obtain(downTime,eventTime,action,x,Math.min(y,((float) contentView.getHeight()) - ((float) 1)),motionEvent.getMetaState());


    return currMotionEvent;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...