APP內網頁跳轉:
xml
<WebView
android:layout_below="@id/lunbo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
/>複製程式碼
java
WebView webView;
private void init(){
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//WebView載入web資源
webView.loadUrl("https://baike.baidu.com/item/%E9%99%88%E4%B8%80%E5%8F%91%E5%84%BF");
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 返回值是true的時候控制去WebView開啟,為false呼叫系統瀏覽器或第三方瀏覽器
return super.shouldOverrideUrlLoading(view, url);
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
// 網頁載入完成
} else {
// 載入中
}
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}複製程式碼
瀏覽器跳轉
java
private void setListener() {
XXX.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("https://www.XXX.com");
intent.setData(content_url);
startActivity(intent);
}
});
}複製程式碼