网络不可用时如何在屏幕上保留AlertDialog

问题描述 投票:0回答:4

我使用以下代码显示AlertDialog并提示用户按“重试”。对话框应保留在屏幕上,直到连接可用。该应用程序正常工作,当网络不可用时,对话框出现。

mProgressBar = new ProgressBar(MainGroupActivity.this);
        AlertDialog.Builder builder = new AlertDialog.Builder(MainGroupActivity.this);
        builder.setTitle("Not Connected")
                .setIcon(R.drawable.disconnect)
                .setView(mProgressBar)
                .setMessage("You are not connected to the Internet")
                .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //Retry connection
                        if(isNetworkAvailable ())
                            mDialog.dismiss();
                    }
                });
        mDialog = builder.create();
        if( !isNetworkAvailable() )
            mDialog.show();

问题是,只要我触摸屏幕上的某个位置或按“重试”,对话框就会被解除!我怎么能防止这种情况?

android networking
4个回答
1
投票

你可以使用setCanceledOnTouchOutside(false)

 AlertDialog mDialog = builder.create();
    mDialog.setCanceledOnTouchOutside(false);
        mDialog.show();

但这样可以防止只有外部触摸而不是正面或负面按钮点击。它的AlertDialog的默认行为是关闭任何按钮上的对话框,无论你是否调用dismiss()。因此,如果你想要克服这种行为,你可以做这样的事情。

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Not Connected")
            .setIcon(R.drawable.ic_play_icon)
            .setView(R.layout.item_dialog)
            .setCancelable(false)
            .setMessage("You are not connected to the Internet");
    final AlertDialog mDialog = builder.create();
    mDialog.setCanceledOnTouchOutside(false);
    mDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialogInterface) {
            Button button =  mDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Do your validations task here

                }
            });
        }
    });
    mDialog.show();

1
投票

setCancelable(false)将适合你。

AlertDialog.Builder builder = new AlertDialog.Builder(MainGroupActivity.this);
    builder.setTitle("Not Connected")
            .setIcon(R.drawable.disconnect)
            .setView(mProgressBar)
            .setCancelable(false)
            .setMessage("You are not connected to the Internet")
            .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //Retry connection
                    if(isNetworkAvailable ())
                        mDialog.dismiss();
                }
            });

0
投票

在你的setCancelable(false)建设上添加alertDialog

mProgressBar = new ProgressBar(MainGroupActivity.this);
        AlertDialog.Builder builder = new AlertDialog.Builder(MainGroupActivity.this);
        builder.setTitle("Not Connected")
                .setIcon(R.drawable.disconnect)
                .setView(mProgressBar)
                .setMessage("You are not connected to the Internet")
                .setCancelable(false)
                .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //Retry connection
                        if(isNetworkAvailable ())
                            mDialog.dismiss();
                    }
                });
        mDialog = builder.create();
        if( !isNetworkAvailable() )
            mDialog.show();

0
投票

要防止对话框在按下后退键时被解除,请使用此选项

dialog.setCancelable(false);

并且为防止对话框在外部触摸时被解雇,请使用此功能

 dialog.setCanceledOnTouchOutside(false);

如需进一步的帮助,请看看这个问题:Prevent Android activity dialog from closing on outside touch

© www.soinside.com 2019 - 2024. All rights reserved.