2014-10-03 34 views
0

好傢伙我試圖讓在網頁視圖後退按鈕回去當按下一個時間和警報完成的活動按兩次當Android的網頁視圖回壓點擊回去雙擊退出

問題是,當我嘗試雙擊它運行,就好像我點擊了一個..他不期望雙擊運行時間

謝謝!

代碼

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

回答

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); 
} 
+0

差不多!我真的需要雙擊工作,即使webview可以返回 ,因此您可以將活動保留在導航中。感謝您的快速回答 – Frank021 2014-10-04 05:43:04

+0

,但我還需要在goback上單擊作品。 – Frank021 2014-10-04 15:41:10

+0

更新了我的答案,看看這是否適合你。 – Chitrang 2014-10-04 18:33:48

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); 
} 
+0

一個好的答案總是會有一個解釋,說明做了什麼以及爲什麼這樣做,不僅是爲了OP,而且是爲了將來SO的訪問者。請添加一些描述,以使其他人瞭解。 – 2017-09-04 09:21:09