如何在Android中发送指针事件

Qin*_* Xu 7 events android android-softkeyboard

我正试图在Android中检测虚拟键盘高度.

我发现了类似的话题:在Android中获取虚拟键盘的高度

似乎作者找到了一种检测高度的方法:

我找到了一种方法来获得它.在我请求打开虚拟键盘后,我发送了我生成的指针事件.它们的y坐标从设备高度开始减小.

我不明白该怎么做.

Vik*_*ram 3

我将使用您发布的链接中提供的代码:

// Declare Variables

int softkeyboard_height = 0;
boolean calculated_keyboard_height;
Instrumentation instrumentation;

// Initialize instrumentation sometime before starting the thread
instrumentation = new Instrumentation();
Run Code Online (Sandbox Code Playgroud)

mainScreenView是你的基本视图,你的活动的视图。m(ACTION_DOWN) 和m1(ACTION_UP) 是使用 调度的触摸事件Instrumentation#sendPointerSync(MotionEvent)。其逻辑是,调度到键盘显示位置的 MotionEvent 将导致以下结果SecurityException

java.lang.SecurityException:注入到另一个应用程序需要 INJECT_EVENTS 权限

y因此,我们从屏幕底部开始,并在循环的每次迭代中向上(通过递减)。对于一定次数的迭代,我们将得到一个 SecurityException(我们将捕获它):这意味着 MotionEvent 正在键盘上发生。时刻y变得足够小(当它位于键盘上方时),我们将打破循环并使用以下方法计算键盘的高度:

softkeyboard_height = mainScreenView.getHeight() - y;
Run Code Online (Sandbox Code Playgroud)

代码:

Thread t = new Thread(){
        public void run() {
            int y = mainScreenView.getHeight()-2;
            int x = 10;
            int counter = 0;
            int height = y;
            while (true){
                final MotionEvent m = MotionEvent.obtain(
                        SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(),
                        MotionEvent.ACTION_DOWN,
                        x, 
                        y,
                        1);
                final MotionEvent m1 = MotionEvent.obtain(
                        SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(),
                        MotionEvent.ACTION_UP,
                        x, 
                        y,
                        1);
                boolean pointer_on_softkeyboard = false;
                try {
                    instrumentation.sendPointerSync(m);
                    instrumentation.sendPointerSync(m1);
                } catch (SecurityException e) {
                    pointer_on_softkeyboard = true;
                }
                if (!pointer_on_softkeyboard){
                    if (y == height){
                        if (counter++ < 100){
                            Thread.yield();
                            continue;
                        }
                    } else if (y > 0){
                        softkeyboard_height = mainScreenView.getHeight() - y;
                        Log.i("", "Soft Keyboard's height is: " + softkeyboard_height);
                    }
                    break;
                }
                y--;

            }
            if (softkeyboard_height > 0 ){
                // it is calculated and saved in softkeyboard_height
            } else {
                calculated_keyboard_height = false;
            }
        }
    };
    t.start();
Run Code Online (Sandbox Code Playgroud)

Instrumentation#sendPointerSync(MotionEvent)

调度指针事件。在接收者从其事件处理返回后的某个时刻完成,尽管它可能尚未完全完成对事件的反应 - 例如,如果它需要更新其显示作为结果,它可能仍在执行过程中那。