2016-05-30 3 views
0

enter image description here私はそのwebviewのアクティビティでwebviewを使用していますが、タップ可能ですが動作していない戻るボタンが1つあります。任意のアイデアwebviewボタンのこのクリックを処理する方法。webviewのwebview backボタンが前のアクティビティ/ページに移動していません

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のテキストボタンが1つあります。 –

+0

私の更新を確認してください – Max

+0

これは答えではありません私はwebView.client()のonPagefinished()の中にコードを書くべきだと思います。 –

0

あなたがWebInterfaceを作成する(あなたが欲しいもの、それを名)とあなたのWebViewからJavascriptInterface

として追加する必要があります、ネイティブコードへのあなたのWebページからの通信にこれらについては

WebView.addJavaScriptInterface()を使用することができます今

としてあなたの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(); 
     } 
    } 
} 

ため

Android.goBack():あなたはこのような何かを行うことができ、ウェブページに、ボタンのクリックでexmaple用

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

今、あなたのWebページであなたは私たちがInterfaceで渡しているAndroidオブジェクトを使用してこのメ​​ソッドを呼び出すことができ、

関連する問題