在Android上关闭键盘后返回沉浸式模式

时间:2014-05-04 12:20:49

标签: android keyboard android-4.4-kitkat

我将沉浸式模式添加到我的应用中。这是代码:

 @Override
 public void onWindowFocusChanged(boolean hasFocus) {
     super.onWindowFocusChanged(hasFocus);
     if (hasFocus)
     {
         getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                 | View.SYSTEM_UI_FLAG_FULLSCREEN
                 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
     }
 }

但如果我在键盘上输入并关闭它(使用后退按钮,通过点击屏幕),导航栏会一直显示,我需要缩小/重新打开应用程序以恢复沉浸式模式。

关闭键盘后如何返回沉浸式模式?

编辑:这是一个Cordova应用

5 个答案:

答案 0 :(得分:1)

这个主题:Immersive mode navigation becomes sticky after volume press or minimise-restore涵盖了类似的问题。

"延迟"解决方案提出应该适用于您的情况。

我偶然发现了同样的问题以及当您在应用程序之间切换导航栏时也不会隐藏的问题。我写了一些指南如何解决这个问题,以便将事情保持在一起:http://vitiy.info/small-guide-how-to-support-immersive-mode-under-android-4-4/

简而言之:我合并了#34;延迟处理程序"有了这个:

public void restoreTransparentBars()
{
    if (isApplicationInImmersiveMode)
        try {
            Window w = activity.getWindow();
            w.getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    );

            w.getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

    } catch (Exception e) {}
}

答案 1 :(得分:1)

这不是最好的解决方案,但绝对是最简单的解决方案,对我有用。

尝试一下:

final Handler forceImmersive = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {

            // Enables regular immersive mode.
            // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
            // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                            // Set the content to appear under the system bars so that the
                            // content doesn't resize when the system bars hide and show.
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            // Hide the nav bar and status bar
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN);

            forceImmersive.postDelayed(this, 1000);
        }
    };

    forceImmersive.postDelayed(runnable, 1000);

答案 2 :(得分:0)

这是你的回答https://stackoverflow.com/a/5993196/772428

您需要在背面按下重新启用沉浸式模式。这是该帖子的代码。

public class EditTextBackEvent extends EditText {

    private EditTextImeBackListener mOnImeBack;

    public EditTextBackEvent(Context context) {
        super(context);
    }

    public EditTextBackEvent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
       if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString());
        }
        return super.dispatchKeyEvent(event);
    }

    public void setOnEditTextImeBackListener(EditTextImeBackListener listener) {
        mOnImeBack = listener;
    }

    public interface EditTextImeBackListener {
       public abstract void onImeBack(EditTextBackEvent ctrl, String text);
    }

}

答案 3 :(得分:0)

试试this。我搜索了3个多小时,这个解决方案效果很好。我希望它会有用。

答案 4 :(得分:0)

我使用处理程序来检测用户的不活动状态,然后隐藏系统ui。它会自动检测用户是否未在屏幕上进行交互,然后在5秒钟后自动隐藏系统用户界面

//Declare handler
private var timeoutHandler: Handler? = null
private var interactionTimeoutRunnable: Runnable? = null

在onCreate()

  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
. . .

       //Initialise handler
        timeoutHandler =  Handler();
        interactionTimeoutRunnable =  Runnable {
            // Handle Timeout stuffs here
            hideSystemUI()
        }

        //start countdown
        startHandler()

. . .

处理焦点更改的方法

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) hideSystemUI()
    }

隐藏系统Ui

    private fun hideSystemUI() {
        // Enables regular immersive mode.
        // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
        // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
                // Set the content to appear under the system bars so that the
                // content doesn't resize when the system bars hide and show.
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                // Hide the nav bar and status bar
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }


    // Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
    private fun showSystemUI() {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    }



// reset handler on user interaction
override fun onUserInteraction() {
    super.onUserInteraction()
    resetHandler()
}

//restart countdown
fun resetHandler() {
    timeoutHandler!!.removeCallbacks(interactionTimeoutRunnable);
    timeoutHandler!!.postDelayed(interactionTimeoutRunnable, 5*1000); //for 10 second

}

// start countdown
fun startHandler() {
    timeoutHandler!!.postDelayed(interactionTimeoutRunnable, 5*1000); //for 10 second
}