我想从onPause()方法中删除我的警报框,因为我正在检查Internet连接,如果没有连接,我会显示一个警报框,说没有连接。但是当我最小化该应用程序并重新启动它时,那里存在前一个警报框,因此总共要有两个警报框,我想避免这种情况。因此,如果我在onPause()方法中将其关闭,第二个警报框不会上来。

有人可以帮我吗,谢谢。

   if (!haveNetworkConnection) {
            // Toast.makeText(getApplicationContext(), "No Connection",
            // Toast.LENGTH_SHORT).show();
            advtlayout.setVisibility(View.GONE);
            System.out.println("no network");
            showGraphOverlay();
            hideOutputTextView();
            hideProgressBar();
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                    InterPreterWithAnimation.this);

            // Setting Dialog Title
            alertDialog.setTitle("No Network");

            // Setting Dialog Message
            alertDialog.setMessage("Turn on Wifi or Mobile data");

            // Setting Icon to Dialog
            // alertDialog.setIcon(R.drawable.);

            // Setting Positive "Yes" Button
            alertDialog.setPositiveButton("Settings",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {

                            // Write your code here to invoke YES event

                            InterPreterWithAnimation.this
                                    .startActivity(new Intent(
                                            Settings.ACTION_SETTINGS));

                        }
                    });

            // Setting Negative "NO" Button
            alertDialog.setNegativeButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // Write your code here to invoke NO event
                            dialog.cancel();
                        }
                    });

            // Showing Alert Message
            alertDialog.show();

最佳答案

尝试通过以下方式创建AlertDialog

        AlertDialog.Builder mAlertDialogBuilder = new Builder(mContext);
        mAlertDialogBuilder.setTitle("Your title here...");
        mAlertDialogBuilder.setMessage("Your message here...");
        mAlertDialogBuilder.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {

                    }
                });

        mAlertDialog = mAlertDialogBuilder.create();
        mAlertDialog.show();


然后,您可以在onPause()内部将其解散为:

if(mAlertDialog.isShowing())
{
    mAlertDialog.dismiss();
}


另外,将您的AlertDialog变量声明为您的类成员。

关于android - 如何关闭警报框?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21569016/

10-10 04:06