很多APP都嵌套了webview,腾讯的webview的简单使用。
腾讯webview的sdk下载地址
用法:
1、布局文件:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<com.xinggui.wz.chuangjiaoplatform.view.MyCustomViewTitleLayout
android:id="@+id/custom1_webview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="3dp"
android:progressDrawable="@drawable/progressbar"
android:visibility="gone"/>
<com.tencent.smtt.sdk.WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</LinearLayout>
2、再就是onCreate里面的代码:
myWebView = (WebView) findViewById(R.id.webview)
progressBar = (ProgressBar) findViewById(R.id.progressBar1)
initWebView()
String url = "你的URL"
myWebView.loadUrl(url)
private void initWebView() {
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String s) {
webView.loadUrl(s);
return true;
}
});
myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView webView, String s, String s1, JsResult jsResult) {
new AlertDialog.Builder(WebViewActivity.this).setTitle("提示消息").setMessage(s1).setPositiveButton("OK", null).show();
jsResult.confirm();
return true;
}
@Override
public void onProgressChanged(WebView webView, int newProgress) {
super.onProgressChanged(webView, newProgress);
if (newProgress == 100) {
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(newProgress);
}
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
项目就不贴出来了,代码很齐全,直接新建项目拷贝就能用