Android Webview后退一次单击返回并双击退出

时间:2014-10-03 17:07:53

标签: android webview

Hello Guys我试图让webview上的后退按钮在按下一次时返回,并在按下两次时以警报结束活动

问题在于,当我尝试双击时,就好像我点击了一个......他没想到双击的运行时间

谢谢!

代码

      public void onBackPressed()
     {
      if (back_pressed + 1000 > System.currentTimeMillis()){
         new AlertDialog.Builder(this)
            .setIcon(R.drawable.ic_launcher)
            .setTitle("Esmalteria Cariúcha")
            .setMessage("Sair do Sistema?")
            .setPositiveButton("Sim", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();    
                }

            })
            .setNegativeButton("Não", null)
            .show();  
        }
        else{
            if(webview.canGoBack()){
                webview.goBack();
            }
        }
        back_pressed = System.currentTimeMillis();
}

2 个答案:

答案 0 :(得分:3)

尝试做到这一点,看看这是否适合你,

boolean doubleBackToExitPressedOnce;

@Override
public void onBackPressed() {
    Log.i(TAG, "onBackPressed");

    if (doubleBackToExitPressedOnce) {
        Log.i(TAG, "double click");
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("Esmalteria Cariúcha")
                .setMessage("Sair do Sistema?")
                .setPositiveButton("Sim",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                finish();
                            }

                        }).setNegativeButton("Não", null).show();
        return;
    } else {
        Log.i(TAG, "single click");
        if (webview.canGoBack()) {
            Log.i(TAG, "canGoBack");
            webview.goBack();
        } else {
            Log.i(TAG, "nothing to canGoBack");
        }
    }

    this.doubleBackToExitPressedOnce = true;
    if (getApplicationContext() == null) {
        return;
    } else {
        Toast.makeText(this, "Please click BACK again to exit",
                Toast.LENGTH_SHORT).show();
    }
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);
}

答案 1 :(得分:0)

试试这个

private WebView view;
private String urlhome = "http://localhost/Default.aspx";
private static String currenturl = "";
private Boolean close_app = false;

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (view.canGoBack()) {
            String url = view.getUrl();

            if (url.equalsIgnoreCase(urlhome)) {
                if (close_app) {
                    // pressed twice
                    finish();
                } else {
                    Toast.makeText(this, "Press Back again to Exit.",
                            Toast.LENGTH_SHORT).show();
                    close_app = true;
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            close_app = false;
                        }
                    }, 3 * 1000);

                    return true;
                }
            } else {
                view.goBack(); //method goback()
                return true;
            }
        } else {
            String url = view.getUrl();
            if (url.equalsIgnoreCase(urlhome)) {
                if (close_app) {
                    // pressed twice
                    finish();
                } else {
                    Toast.makeText(this, "Press Back again to Exit.",
                            Toast.LENGTH_SHORT).show();
                    close_app = true;
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            close_app = false;
                        }
                    }, 3 * 1000);

                    return true;
                }
            }else {
                view.loadUrl(urlhome);
                currenturl = urlhome;
                return true;
            }
        }
    }
    return super.onKeyDown(keyCode, event);
}