我无法解决的简短问题,我想在人们单击超链接后从我的 WebView 启动一个新浏览器。但是我怎样才能设置那个链接的目标来逃避 WebViewClient 呢?

这是我的代码,非常感谢任何帮助:

WebView site = (WebView)findViewById(R.id.WebView);
  site.setWebViewClient(new WebViewClient());
  site.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
  site.getSettings().setJavaScriptEnabled(true);

  button1.setOnClickListener(button1OnClickListener);
  button2.setOnClickListener(button2OnClickListener);
  button3.setOnClickListener(button3OnClickListener);
  button4.setOnClickListener(button4OnClickListener);


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

  progressBar = ProgressDialog.show(FlitsersActivity.this, "Thingy1", "Load...", false, true);

  site.setWebViewClient(new WebViewClient() {
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
       Log.i(TAG, "Loading...");
       view.loadUrl(url);
       return true;
   }

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

   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
       Log.e(TAG, "Error: " + description);
       Toast.makeText(MyActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
       alertDialog.setTitle("Error");
       alertDialog.setMessage(description);
       alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
               return;
           }
       });
       alertDialog.show();
   }
 });

 site.loadUrl("http://www.etcetera.com");

}

最佳答案

正是,谢谢!我想我是在你写第二个回复的时候发现的;)

现在我正在使用下面的代码:它就像一个魅力:) 非常感谢您的回复!

site.setWebViewClient(new WebViewClient() {
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
       String myAlternativeURL = "http://newURL";
       if (!url.equals(myAlternativeURL)) {
           {
               Intent i = new Intent(Intent.ACTION_VIEW);
               i.setData(Uri.parse("http://newURL"));
               startActivity(i);
               }
           return true;
       } else {
       Log.i(TAG, "Loading...");
       view.loadUrl(url);
       return true;
   }
   }

10-08 13:50