腾讯TBS X5 WebView的简单使用
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();//初始化webview的设置,注释写的很清楚
- String url = "你的URL"
- myWebView.loadUrl(url);
- private void initWebView() {
- WebSettings webSettings = myWebView.getSettings();
- webSettings.setJavaScriptEnabled(true);//支持js
- // myWebView.requestFocusFromTouch();//如果webView中需要用户手动输入用户名、密码或其他,则webview必须设置支持获取手势焦点
- webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //允许js弹窗
- 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(); //不调用,alert只弹出一次
- 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) {
- //如果不做任何处理,浏览网页,点击系统“Back”键,整个Browser会调用finish()而结束自身,
- // 如果希望浏览的网 页回退而不是推出浏览器,需要在当前Activity中处理并消费掉该Back事件。
- if (keyCode == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) {
- myWebView.goBack();
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
扫描二维码,手机查看
声明:本文来源于互联网,观点仅代表作者本人,不代表欢乐你我,真实性请妥善甄别。