首页 > 解决方案 > How to hide footer component that appear on keyboard's top when editText gets focused in fragment?

问题描述

enter image description here

Fragment layout enter image description here


Actually the footer is set inside an activity class and the edittext is placed inside a fragment. manifest file

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

Inside my fragment class I added

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

These are the code snippet that I used, but it won't work! Note: in Fragment UI, the whole screen has a scroll view, Don't know why it is happening like this?Actually I don't want my footer on the keyboard's top.
Any suggestions on how to solve this behavior? And its appriciatble for the replies :)

标签: androidandroid-fragmentsuiscrollview

解决方案


将此添加到您的活动中。

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

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

推荐阅读