有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java警报对话框相关问题

我已经实现了AlertDialog框,它在没有互联网的情况下显示没有互联网连接,但当我尝试单击后退按钮时,AlertDialog框会一次又一次弹出,尽管返回到Android Studio中以前的活动。我希望当AlertDialog框出现时,没有互联网连接,用户点击后退按钮,然后返回主活动。提前谢谢

Notify.java

import 安卓.app.Activity;
import 安卓.app.AlertDialog;
import 安卓.app.ProgressDialog;
import 安卓.content.DialogInterface;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.util.Log;
import 安卓.view.KeyEvent;
import 安卓.view.View;

private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.notifyavtivity);

    this.webview = (WebView) findViewById(R.id.webview);

    getSupportActionBar().hide();

    WebSettings webSettings = webview.getSettings();
    webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    webview.getSettings().setAppCacheEnabled(true);
    webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
    webSettings.setDomStorageEnabled(true);
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    webSettings.setUseWideViewPort(true);
    webSettings.setSaveFormData(true);
    webSettings.setEnableSmoothTransition(true);
    webSettings.setJavaScriptEnabled(true);
    webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

    progressBar = ProgressDialog.show(notify.this, "Please wait", "Loading...");
    progressBar.setCancelable(true);
    webview.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i(TAG, "Processing webview url click...");
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            Log.i(TAG, "Finished loading URL: " + url);
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.e(TAG, "Error: " + description);
            webview.loadUrl("about:blank");
            alertDialog.setTitle("Error");
            alertDialog.setMessage("No Internet Connection\nTry Again.");
            alertDialog.setButton("OK", new  DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            });
            alertDialog.show();
        }
    });
    webview.loadUrl("https://www.google.com");
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {

            case KeyEvent.KEYCODE_BACK:
                if (webview.canGoBack()) {
                    webview.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }

    return super.onKeyDown(keyCode, event);
}

共 (1) 个答案

  1. # 1 楼答案

    改变

    DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            });
    

    DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });