【问题标题】:Hide action bar on soft keyboard popup in fragment在片段中隐藏软键盘弹出窗口上的操作栏
【发布时间】:2016-10-03 07:19:32
【问题描述】:

我想在软键盘弹出窗口上隐藏操作栏。我已经使用以下代码的代码来获得结果。但是当我想回去时,应用程序崩溃了。我已经包含在哪一行我也得到了错误。请指导我完成此操作。

提前致谢。

private void createView() {

    mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {

            Rect rect = new Rect();
            //rect will be populated with the coordinates of your view that area still visible.

            mRootView.getWindowVisibleDisplayFrame(rect);
            int heightDiff = mRootView.getRootView().getHeight() - (rect.bottom - rect.top);

            if (heightDiff > 100) {

                // if more than 100 pixels, its probably a keyboard...
                //keyboard visible
                mBinding.healthRecordContainer.setVisibility(View.GONE);
                ((AppCompatActivity) getActivity()).getSupportActionBar().hide();

            } else {

                //keyboard not visible
                mBinding.healthRecordContainer.setVisibility(View.VISIBLE);
                ((AppCompatActivity) getActivity()).getSupportActionBar().show(); // app crashes
            } //java.lang.NullPointerException
        }
    });
}

【问题讨论】:

标签: android android-actionbar


【解决方案1】:

您可以使用以下代码。然后应用程序不会崩溃。

Activity activity = getActivity();

if (activity instanceof ActionBarActivity) {

    ActionBarActivity actionBarActivity = (ActionBarActivity) activity;

    ActionBar actionBar = actionBarActivity.getSupportActionBar();

    if (actionBar != null) {
        actionBar.hide();
    }
}

【讨论】:

    【解决方案2】:

    只在清单中声明活动的地方做一件事。 例如

    <activity
                android:name=".Your Activity here"
                android:windowSoftInputMode="stateVisible|adjustResize"
                >
            </activity>
    

    【讨论】:

    • 这是隐藏软键盘吗?
    猜你喜欢
    • 2016-02-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 2011-12-17
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多