How to capture the “virtual keyboard show/hide” event in Android?

前端 未结 16 1471
暗喜
暗喜 2020-11-22 07:23

I would like to alter the layout based on whether the virtual keyboard is shown or not. I\'ve searched the API and various blogs but can\'t seem to find anything useful.

16条回答
  •  借酒劲吻你
    2020-11-22 07:57

    You can also check for first DecorView's child bottom padding. It will be set to non-zero value when keyboard is shown.

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        View view = getRootView();
        if (view != null && (view = ((ViewGroup) view).getChildAt(0)) != null) {
            setKeyboardVisible(view.getPaddingBottom() > 0);
        }
        super.onLayout(changed, left, top, right, bottom);
    }
    

提交回复
热议问题