简体   繁体   English

获取Android中虚拟键盘的高度

[英]Get the height of virtual keyboard in Android

How can I get the height of virtual keyboard in Android? 如何获得Android中虚拟键盘的高度? Is it possible? 可能吗?

I try to get it from the main window, but it gives me full height of the application. 我尝试从主窗口获取它,但是它使我拥有了整个应用程序的高度。 But I want to get the keyboard height. 但是我想获得键盘高度。

您无法获取键盘的高度,但是您可以获取您真正想要的View的高度-您将获得提供给onLayout调用的数据到当前视图中。

This solution is also hacky but solve the problem (atleast for me). 此解决方案也很麻烦,但可以解决问题(对我来说至少)。

  1. I have places on temporary view with transparent background at the bottom of the screen. 我在屏幕底部的透明背景下的临时视图中放了一些地方。 So this view will be invisible. 因此,该视图将是不可见的。
  2. I have added android:windowSoftInputMode="adjustResize" flag in activity tag in manifest. 我已经在清单中的活动标记中添加了android:windowSoftInputMode="adjustResize"标志。
  3. Now main story is in onGlobalLayout() . 现在主要的故事在onGlobalLayout() There i calculate the difference between the y axis of temp view and height of root view 我在那里计算了临时视图的y轴与根视图的高度之间的差

    final View view = findViewById(R.id.base); 最终视图view = findViewById(R.id.base); view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { view.getViewTreeObserver()。addOnGlobalLayoutListener(new OnGlobalLayoutListener(){

     @Override public void onGlobalLayout() { int rootViewHeight = view.getRootView().getHeight(); View tv = findViewById(R.id.temp_view); int location[] = new int[2]; tv.getLocationOnScreen(location); int height = (int) (location[1] + tv.getMeasuredHeight()); deff = rootViewHeight - height; // deff is the height of soft keyboard } 

    }); });

If you don't want android:windowSoftInputMode="adjustResize" in your app. 如果您不想在应用中使用android:windowSoftInputMode="adjustResize" You can try something like this: 您可以尝试如下操作:

    any_view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int height = main_layout.getHeight();
            Log.w("Foo", String.format("layout height: %d", height));
            Rect r = new Rect();
            main_layout.getWindowVisibleDisplayFrame(r);
            int visible = r.bottom - r.top;
            Log.w("Foo", String.format("visible height: %d", visible));
            Log.w("Foo", String.format("keyboard height: %d", height - visible));
        }
    });

Let's suppose that the rootView of your layout is RelativeLayout. 假设布局的rootView是RelativeLayout。 What you can do create a class CustomRelativeLayout which extends RelativeLayout and Overrides onSizeChanged Method inside it. 您可以创建一个CustomRelativeLayout类,该类扩展了RelativeLayout并覆盖了其中的onSizeChanged方法。 So when the soft keyboard opens up, the height of the RelativeLayout will change and the change will be notified inside the onSizeChanged Method. 因此,当软键盘打开时,RelativeLayout的高度将更改,并且更改将在onSizeChanged方法内通知。

public class CustomRelativeLayout extends RelativeLayout {

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    int softKeyboardHeight = oldh - h; // Assuming softKeyboard Opened up
    // oldh is the height when the layout was covering the whole screen
    // h is the new height of the layout when the soft keyboard opened up

    if(softKeyboardHeight > oldh * 0.15) {
        Log.i("Here", Integer.toString(softKeyboardHeight));
        // Keyboard has popped up

    } else {
        // Not the keyboard
    }
}

In your Manifest file make these changes so that the Activity opens in Resize Mode and Not pan and scan mode. 在清单文件中进行这些更改,以使“活动”在“调整大小模式”和“不平移和扫描”模式下打开。 In Resize mode the Layout will be able to resize when keybaord opens up. 在“调整大小”模式下,打开键盘时,布局将能够调整大小。 To read more about pan-scan and Resize visit https://developer.android.com/training/keyboard-input/visibility 要了解有关全景扫描和调整大小的更多信息,请访问https://developer.android.com/training/keyboard-input/visibility

<activity
        android:name=".MainActivity"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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();

Try this 尝试这个

KeyboardView keyboardView = new KeyboardView(getApplicationContext(), null);
int height = (keyboardView.getKeyboard()).getHeight();
Toast.makeText(getApplicationContext(), height+"", Toast.LENGTH_LONG).show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM