調用loadurl時,Android Webview啓動瀏覽器

本文翻譯自: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;
}                                                                                               }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章