android 使用webview載入網頁問題

來源:互聯網
上載者:User

標籤:效果   資料   this   xtend   state   ping   msu   cti   should   

我在做圖片站時要將其移動站打包成app,

我使用的是webview來載入網頁(本來想使用phonegap來做來,但是還是太麻煩了),

具體代碼如下:

 

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);WebView webView=new WebView(this);        webView.loadUrl("http://www.mimi199.com/");        Intent intent= getIntent();        setContentView(webView);    }

搞定

 

 

等等,首頁效果是不錯,但是出現了一個問題,當點擊某個超連結,app會自動呼叫瀏覽器來開啟新的頁面,這可不是我想要的功能,我希望的是開啟的時候應該是每個頁面都在app中開啟,關瀏覽器神馬事啊!

查了好多資料,原來還有個WebviewClient的類

只需要再寫個類來即成WebviewClient

代碼如下

 

public class MyWebViewClient extends WebViewClient {    public boolean shouldOverrideUrlLoading(WebView webView,String url){        webView.loadUrl(url   );//就是這句話,讓每個超連結都在app的webview開啟,而不是瀏覽器        return true;    }}
然後稍微改下MainActivity
     protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);WebView webView=new WebView(this);        webView.loadUrl("http://www.mimi199.com/);
//多加上這句話就可以了        webView.setWebViewClient(new MyWebViewClient());     }

android初學者與大家共勉

 

 

android 使用webview載入網頁問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.