Get the height of virtual keyboard in Android

前端 未结 6 1433
有刺的猬
有刺的猬 2020-12-01 14:31

How can I get the height of virtual keyboard in Android? Is it possible?

I try to get it from the main window, but it gives me full height of the application. But I

6条回答
  •  时光取名叫无心
    2020-12-01 15:26

    you can use this sample code. it is dirty solution but it works

    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,
                                INTERNAL_POINTER_META_STATE);
                        final MotionEvent m1 = MotionEvent.obtain(
                                SystemClock.uptimeMillis(),
                                SystemClock.uptimeMillis(),
                                MotionEvent.ACTION_UP,
                                x, 
                                y,
                                INTERNAL_POINTER_META_STATE);
                        boolean pointer_on_softkeyboard = false;
                        try {
                            getSingletonInstrumentation().sendPointerSync(m);
                            getSingletonInstrumentation().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;
                            }
                            break;
                        }
                        y--;
    
                    }
                    if (softkeyboard_height > 0 ){
                        // it is calculated and saved in softkeyboard_height
                    } else {
                        calculated_keyboard_height = false;
                    }
                }
            };
            t.start();
    

提交回复
热议问题