调用loadurl时,Android Webview启动浏览器

在Android应用中,作者遇到一个Webview在调用loadUrl时启动设备浏览器而不是在自身内显示页面的问题。根据StackOom上的讨论,解决方案是实现WebClient并在加载URL之前设置它。帖子提供了简单的实现代码示例以及使用ChromeClient进行更复杂处理的建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Android webview launches browser when calling loadurl

I created an Activity that has a title and a web view in a LinearLayout . 我在LinearLayout创建了一个具有标题和Web视图的Activity In the onResume() method it calls webView.loadUrl(url) . onResume()方法中,它调用webView.loadUrl(url) The problem is that the activity first shows the title with the rest of the screen blank, then the device browser is launched with the page for the URL. 问题在于活动首先显示标题,其余屏幕为空白,然后启动设备浏览器并显示URL页面。 What I want to see is the page being shown in the WebView below the title. 我想看到的是标题下方的WebView中显示的页面。 What could be the problem? 可能是什么问题呢?

Edit : Ok, did some further search and found this one: 编辑 :好的,做了一些进一步的搜索,找到了这个:

Clicking URLs opens default browser 单击URL将打开默认浏览器

It points to the WebView tutorial here . 它指向此处WebView教程。

Just implement the web client and set it. 只需实现Web客户端并进行设置即可。


#1楼

参考:https://stackoom.com/question/WVC5/调用loadurl时-Android-Webview启动浏览器


#2楼

Answering my question based on the suggestions from Maudicus and Hit. 根据Maudicus和Hit的建议回答我的问题。

Check the WebView tutorial here . 此处查看WebView教程。 Just implement the web client and set it before loadUrl . 只需实现Web客户端并在loadUrl之前进行设置即可 The simplest way is: 最简单的方法是:

myWebView.setWebViewClient(new WebViewClient());

For more advanced processing for the web content, consider the ChromeClient. 要对网络内容进行更高级的处理,请考虑使用ChromeClient。


#3楼

用这个:

lWebView.setWebViewClient(new WebViewClient());

#4楼

use like this: 像这样使用:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dedline);

    WebView myWebView = (WebView) findViewById(R.id.webView1);
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.loadUrl("https://google.com");
}

#5楼

Try this code... 试试这个代码...

private void startWebView(String url) {

    //Create new webview Client to show progress dialog
    //When opening a url or click on link

    webView.setWebViewClient(new WebViewClient() {      
        ProgressDialog progressDialog;

        //If you will not use this method url links are opeen in new brower not in webview
        public boolean shouldOverrideUrlLoading(WebView view, String url) {              
            view.loadUrl(url);
            return true;
        }

        //Show loader on url load
        public void onLoadResource (final WebView view, String url) {
            if (progressDialog == null) {
                // in standard case YourActivity.this
                progressDialog = new ProgressDialog(view.getContext());
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }
        }
        public void onPageFinished(WebView view, String url) {
            try{
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;
            }
            }catch(Exception exception){
                exception.printStackTrace();
            }
        }

    }); 

     // Javascript inabled on webview  
    webView.getSettings().setJavaScriptEnabled(true); 

    // Other webview options
    /*
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);
    webView.getSettings().setBuiltInZoomControls(true);
    */

    /*
     String summary = "<html><body>You scored <b>192</b> points.</body></html>";
     webview.loadData(summary, "text/html", null); 
     */

    //Load url in webview
    webView.loadUrl(url);
}

#6楼

I was facing the same problem and I found the solution Android's official Documentation about WebView 我遇到了同样的问题,并且找到了解决方案Android的有关WebView的官方文档

Here is my onCreateView() method and here i used two methods to open the urls 这是我的onCreateView()方法,在这里我使用了两种方法来打开网址

Method 1 is opening url in Browser and 方法1是在浏览器中打开url,然后

Method 2 is opening url in your desired WebView. 方法2是在所需的WebView中打开url。
And I am using Method 2 for my Application and this is my code: 我正在为我的应用程序使用方法2,这是我的代码:

public class MainActivity extends Activity {
   private WebView myWebView;

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      View rootView = inflater.inflate(R.layout.fragment_webpage_detail, container, false);

      // Show the dummy content as text in a TextView.
      if (mItem != null) {

         /* Method : 1
          This following line is working fine BUT when we click the menu item then it opens the URL in BROWSER not in WebView */
         //((WebView)   rootView.findViewById(R.id.detail_area)).loadUrl(mItem.url);

        // Method : 2
        myWebView = (WebView) rootView.findViewById(R.id.detail_area); // get your WebView form your xml file
        myWebView.setWebViewClient(new WebViewClient()); // set the WebViewClient
        myWebView.loadUrl(mItem.url); // Load your desired url
    }

    return rootView;
}                                                                                               }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值