2016-05-30 170 views
0

enter image description here我正在webview上使用该webview上的活动,有一个后退按钮(不是键盘上的后退按钮),可点击但不起作用。任何想法如何处理这个点击webview按钮。在webview的webview后退按钮没有导航到之前的活动/页面

CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true);

webView.getSettings().setJavaScriptEnabled(true); 

    webView.setWebViewClient(new WebViewClient() { 
     ProgressDialog progressDialog = 
      ProgressDialog.show(activity, Constants.EMPTY_VALUE, Constants.PLEASE_WAIT, true); 

     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url, headers); 
     return true; 
     } 

     public void onPageStarted(WebView view, String url, Bitmap favicon) { 

     if (activity != null && progressDialog != null) { 
      progressDialog.setCancelable(false); 
     } 

     updatePaymentUrl = null; 
     if (url.contains("callback-data")) { 
      webView.setVisibility(View.GONE); 
      showpDialog(); 
      int indexQ = url.indexOf("?"); 
      partUrl = url.substring(indexQ + 1, url.length()); 
      speedOrderUpdateApi(); 
     } 

     if (updatePaymentUrl != null) { 
      super.onPageStarted(view, updatePaymentUrl, favicon); 
     } else { 
      super.onPageStarted(view, url, favicon); 
     } 
     } 

     public void onReceivedError(WebView view, int errorCode, String description, 
      String failingUrl) { 
     Logger.logInfo("errorCode = ", String.valueOf(errorCode)); 
     Logger.logInfo("description = ", description); 
     Logger.logInfo("failingUrl = ", failingUrl); 
     super.onReceivedError(view, errorCode, description, failingUrl); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
     if (activity != null && progressDialog != null) { 
      if (progressDialog.isShowing()) { 
      progressDialog.dismiss(); 
      } 

      // If title not found hide webview 
      Logger.logInfo("view.getTitle() = ", view.getTitle()); 
      if (view.getTitle() != null && "Not Found".equals(view.getTitle())) { 
      view.setVisibility(View.INVISIBLE); 
      Toast.makeText(activity, "Link Not Found", Toast.LENGTH_LONG).show(); 
      } 
     } 
     } 
    }); 

    // Not needed 
    Random random = new Random(System.currentTimeMillis()); 
    int randomNumber = random.nextInt(); 
    Logger.logInfo("urlTemp = ", url); 
    webView.loadUrl(url, headers); 
    } 

回答

0

WebView没有默认OnBackPress我们用onKeyDown而不是为去前一页,这里是举例来说,你可以把这个代码在你WebView活动

@Override 
public boolean onKeyDown (int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { 

     webView.goBack(); // go back in only the web view 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

更新:

将该代码放在onClick()中用于该按钮

if(webView.canGoBack()){ 
     webView.goBack(); 
    } else { 
     finish(); 
    } 
+0

不..这不是一个解决方案... onKeyDown()按钮工作正常,但在webView我有一个BACK文本按钮,我需要处理that.U可以看到附加的图像。 –

+0

检查我的更新 – Max

+0

不,这不是一个答案我认为我应该在webView.client()的onPagefinished()内编写代码。 –

0

您可以使用WebView.addJavaScriptInterface()从您的网页,本地代码进行沟通,

对于这些,你必须创建一个WebInterface(您想使用任何名字),它作为一个JavascriptInterface添加到您的WebView

为如

public class YourWebInterface { 

    private Context context; 

    public YourWebInterface(Context context) { 
     this.context = context; 
    } 

    @JavascriptInterface 
    public void goBack() { 
     if(webView.canGoBack()){ 
      webView.goBack(); 
     } else { 
      finish(); 
     } 
    } 
} 

这个接口现在添加到您的WebView为

webView.addJavascriptInterface(new YourWebInterface(context), "Android"); 

现在在你的网页,你可以调用使用Android对象,我们正在通过与Interface这种方法,

为〔实施例在网页的点击按钮,你可以做这样的事情:

Android.goBack()