Android隐藏导航栏/保持沉浸模式,软键盘外观

时间:2014-11-24 22:46:31

标签: android view keyboard

使用沉浸式模式在客户端的应用上使用以下代码隐藏每个活动的导航栏和状态栏:

int currentApiVersion = android.os.Build.VERSION.SDK_INT;

final int flags = 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;

// This work only for android 4.4+
if (currentApiVersion >= 19) {

        getWindow().getDecorView().setSystemUiVisibility(flags);
        // Code below is for case when you press Volume up or Volume down.
        // Without this after pressing valume buttons navigation bar will
        // show up and don't hide
        final View decorView = getWindow().getDecorView();
        decorView
                .setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {

                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            decorView.setSystemUiVisibility(flags);
                        }
                }
         });
}

唯一的问题是他们希望应用程序保持沉浸式模式,即使软键盘显示为输入EditText,也不会显示导航栏。任何人都可以想到一种方法,即使在使用键盘时总是隐藏导航按钮(后退/隐藏键盘,家庭等)?

7 个答案:

答案 0 :(得分:6)

我提出了一种解决方法,检查每个内部的导航栏状态,尝试隐藏它并再次检查(并再次)。

这是一段代码,确保导航栏在软键盘关闭后2秒内隐藏。

  private final Runnable checkSystemUiRunnable = new Runnable() {
    @Override
    public void run() {
      checkHideSystemUI();
    }
  };

  private void checkHideSystemUI() {
    // Check if system UI is shown and hide it by post a delayed handler
    if (isSystemUiShown) {
      hideSystemUI();
      handler.postDelayed(checkSystemUiRunnable, SYSTEM_UI_HIDE_DELAY);
    }
  }

  private void hideSystemUI() {
    decorView.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 // hide nav bar
                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE);
  }

  // In onCreate()
    decorView.setOnSystemUiVisibilityChangeListener(
            new View.OnSystemUiVisibilityChangeListener() {
              @Override
              public void onSystemUiVisibilityChange(int visibility) {
                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                  handler.postDelayed(checkSystemUiRunnable, SYSTEM_UI_HIDE_DELAY);
                  isSystemUiShown = true;
                } else {
                  isSystemUiShown = false;
                }
              }
            });

答案 1 :(得分:4)

this。我已经搜索了3个多小时来解决这个问题,而且解决方案运行良好。我希望它会有所帮助。

答案 2 :(得分:2)

这是我的解决方案; 首先,我检查了软键盘是否显示:

getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                Rect r = new Rect();
                getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                int screenHeight = getWindow().getDecorView().getRootView().getHeight();

                int keypadHeight = screenHeight - r.bottom;

                //Log.d(TAG, "keypadHeight = " + keypadHeight);

                if (keypadHeight > screenHeight * 0.15) { 
                     //Keyboard is opened
                     hideNavBar();
                }
                else {
                    // keyboard is closed
                }
            }
        });

我有一个hideNavBar()方法在软键盘出现时被触发。

private void hideNavBar() {
    if (Build.VERSION.SDK_INT >= 19) {
        View v = getWindow().getDecorView();
        v.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);
    }
}

这解决了在输入Edittext的同时获取导航栏的问题。

答案 3 :(得分:0)

已更新

我有一个绘画应用程序(Paint Shapes),这是我以前始终处于沉浸式模式下的配置。我使用onWindowFocusChanged方法。

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        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);
    }
}

答案 4 :(得分:0)

我认为那是不可能的。

Flutter: How to show onscreen keyboard without android's bottom navigation bar on focusing a textfield?

陈述清楚,我的经验似乎证实了这一点。

将以下代码置于活动中

private var appVisibility:Int = (  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                or View.SYSTEM_UI_FLAG_LOW_PROFILE
                                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                or View.SYSTEM_UI_FLAG_FULLSCREEN
                                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    window.decorView.systemUiVisibility = appVisibility

}
override fun onResume() {
    super.onResume()
    updateUI()
}
fun updateUI() {
    val decorView = window.decorView
    decorView.setOnSystemUiVisibilityChangeListener { visibility ->
        if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
            decorView.systemUiVisibility = appVisibility
        }
    }
}

始终正确隐藏导航栏(当您从屏幕底部滑动时,导航栏会显示几秒钟,然后消失),但是在打开键盘时。我还尝试以编程方式打开键盘,然后在几秒钟后隐藏导航栏,但没有成功。

private fun toggleIME(){
    val imm             = applicationContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
    val view: EditText? = text

    if(view != null && imm != null) {
        isKOpen = !isKOpen
        if (isKOpen) {
            view.requestFocus()
            imm.showSoftInput(view, InputMethodManager.SHOW_FORCED)

            handler.postDelayed(Runnable { // execute after XXXms
                updateUI()
                window.decorView.systemUiVisibility = appVisibility
            }, 5000)

        }
        else {
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }
}

我希望能丢失一些东西。对于开发的自定义键盘,我将需要它(必须在其中实现后退按钮)

答案 5 :(得分:0)

要输入软输入而不会失去沉浸式或沉浸式粘滞模式Check this

答案 6 :(得分:0)

最有效的方法是仅在需要时才设置 SystemUIVisibility:

  1. 在你的 onCreate() 中
  2. 当全屏模式丢失时使用 OnSystemUIChangeListener

我使用并推荐的解决方案:

public class BaseActivity extends Activity {

    @SuppressLint("InlinedApi")
    private static final int UI_OPTIONS = View.SYSTEM_UI_FLAG_LOW_PROFILE
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;        

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Step 1
        hideSystemUI();

        // Step 2
        getWindow().getDecorView()
                   .setOnSystemUiVisibilityChangeListener(new View
                   .OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    hideSystemUI();
                }
            }
        });
    }
    
    private void hideSystemUI() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) actionBar.hide();
        getWindow().getDecorView().setSystemUiVisibility(UI_OPTIONS);
    }

}