隐藏系统栏时失去焦点

时间:2013-10-18 09:42:24

标签: android hide system lost-focus

我正在使用显示在锁定屏幕顶部的webview,我也会在很短的时间后自动隐藏系统栏:

/**
 * Class used to hide the device System UI bar.
 * 
 */
public class SystemUiHider {
    /**
     * Delay until to hide the bar.
     */
    private static final int HIDE_DELAY_MILLIS = 3000;

    /**
     * Declare the handler.
     */
    private Handler mHandler;
    /**
     * Declare the view on which to hide the bar.
     */
    private View mView;

    private Utils utils;

    /**
     * Set the class view to be used.
     * 
     * @param view
     */
    public SystemUiHider(final View view, final Context context) {
        this.mView = view;
        this.utils = new Utils(context);

    }

    /**
     * Setup used to hide the system bar and start the handler.
     */
    public final void setup() {
        hideSystemUi();
        this.mHandler = new Handler();
        mView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(final int visibility) {
                if (visibility != View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) {
                    delay();
                }
            }
        });
    }

    /**
     * Set the flag to hidden for the view.
     */
    private void hideSystemUi() {
        utils.hideSystemBar(WebViewActivity.appContext);
    }

    /**
     * Set the runnable that runs on a different thread to call the method
     * hiding the system bar.
     */
    private Runnable mHideRunnable = new Runnable() {
        public void run() {
            hideSystemUi();
        }
    };

    /**
     * Set the delay until to hide the system bar.
     */
    private void delay() {
        mHandler.removeCallbacks(mHideRunnable);
        mHandler.postDelayed(mHideRunnable, HIDE_DELAY_MILLIS);
    }
}

这是隐藏系统栏的方法:

/**
 * Hide system bar.
 * Different methods applied depending on the SDK version
 *
 * @param context
 */
@SuppressLint("InlinedApi")
@SuppressWarnings("deprecation")
public void hideSystemBar(Context context) {
    if (SDK_INT < FOURTEEN) {
        Log.i("WebViewClient", "hideSystemBar: < 14 ");
        ((Activity) context).getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
    } else if (SDK_INT >= FOURTEEN && SDK_INT < SIXTEEN) {
        Log.i("WebViewClient", "hideSystemBar: 14  - 16");
        ((Activity) context).getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE   // 14
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);                                                         // 14   
    } else if (SDK_INT >= SIXTEEN) {
        Log.i("WebViewClient", "hideSystemBar: > 16");
        ((Activity) context).getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE   // 14
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION                                                           // 14
                | View.SYSTEM_UI_FLAG_FULLSCREEN);                                                              // 16
    } 
}

我遇到的问题是我的webview在使用时失去了焦点。我的猜测是因为((Activity) context).getWindow().getDecorView()

是否有另一种方法可以在锁定的屏幕上显示活动并隐藏系统栏而不会失去焦点?

谢谢。

1 个答案:

答案 0 :(得分:0)

经过一番搜索,我最终做到了这一点。需要在其他api上进行测试,但现在重点关注应用程序。

private void hideSystemUI() {
    int visibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LOW_PROFILE;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        visibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        visibility |= View.SYSTEM_UI_FLAG_IMMERSIVE;
    }
    getActivity().getWindow().getDecorView().setSystemUiVisibility(visibility);
}