当 editText 集中在片段中时,如何隐藏出现在键盘上的页脚组件?

How to hide footer component that appear on keyboard's top when editText gets focused in fragment?

片段布局


实际上,页脚设置在 activity class 内,编辑文本放在片段内。 清单文件

 <activity
        android:name="HomeController"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateHidden|adjustPan"/>

在我的片段中 class 我添加了

 getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

这些是我使用的代码片段,但它不起作用! 注意:在FragmentUI中,全屏有滚动视图, 不知道为什么会这样?其实我不希望我的页脚在键盘的顶部。
关于如何解决此行为的任何建议?及其回复的适用性:)

在 xml 文件的父布局中使用 android:windowSoftInputMode="hidden"。如果它有 layoutAbove 属性 然后尝试删除它。

尝试在清单文件中添加以下内容: android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

同时从片段中删除以下内容: getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

将此添加到您的 activity。

我没有测试,但这可能有效。虽然这是一个糟糕的方法(

 contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
    
        Rect r = new Rect();
        View rootView = getWindow().getDecorView().getRootView();
        rootView.getWindowVisibleDisplayFrame(r);
        int screenHeight = rootView.getHeight();
    
        // r.bottom is the position above soft keypad or device button.
        // if keypad is shown, the r.bottom is smaller than that before.
        int keypadHeight = screenHeight - r.bottom;
    
        Log.d(TAG, "keypadHeight = " + keypadHeight);
    
        if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
            // keyboard is opened
            //set bottom navigation(footer) bar to View.GONE
        }
        else {
            // keyboard is closed
           //set bottom navigation bar(footer) to View.VISIBLE
        }
    }
    });