显示AlertDialog时保持全屏

时间:2017-01-18 17:47:20

标签: android focus alertdialog android-statusbar android-navigation-bar

基于以下代码片段,我想知道如何隐藏软键(状态和导航栏)并在整个应用会话期间保持沉浸式模式,即使显示AlertDialog:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    AlertDialog.Builder dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dialog = new AlertDialog.Builder(this);

        findViewById(R.id.button).setOnClickListener(this);
    }

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

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                dialog.setTitle("Title");
                dialog.setMessage("Message");
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {

                        // Dismisses dialog.
                    }
                }).create().show();

                break;
        }
    }
}

...软键在整个应用程序中保持隐藏状态,直到我按下按钮显示软键出现的对话框(将焦点移开),然后在解除对话框后再次隐藏。非常感谢。

2 个答案:

答案 0 :(得分:0)

试试这个:

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button:
            dialog.setTitle("Title");
            dialog.setMessage("Message");
            AlertDialog alert = dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                    // Dismisses dialog.
                }
            }).create();

            alert.getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            alert.show();

            break;
    }
}

或者这个:

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button:
            dialog.setTitle("Title");
            dialog.setMessage("Message");
            AlertDialog alert = dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                    // Dismisses dialog.
                }
            }).create();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alert.getWindow().getDecorView()
                    .setSystemUiVisibility(
                          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);
            }
            alert.show();

            break;
    }
}

答案 1 :(得分:0)

使用以下代码创建您自己的沉浸式 AlertDialog,并使用 alertDialog.showImmersive()

调用它
const val FLAGS_FULLSCREEN =
    View.SYSTEM_UI_FLAG_LOW_PROFILE or
            View.SYSTEM_UI_FLAG_FULLSCREEN or
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

fun AlertDialog.showImmersive() {
    // Set the dialog to not focusable
    window?.setFlags(
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)

    // Make sure that the dialog's window is in full screen
    window?.decorView?.systemUiVisibility = FLAGS_FULLSCREEN

    // Show the dialog while still in immersive mode
    show()

    // Set the dialog to focusable again
    window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
}