概要
WebView控件自定义浏览器,在加载网页的时候,禁止它调用默认浏览器打开,同时设置WebView为默认浏览器,当用户点击了你的WebView中的一个链接,可以在你的WebView中设置这一行为,使得连接仍在你的WebView中打开,调用setWebViewClient()方法。
配置默认浏览器
- mWebView = (ProgressWebView) findViewById(R.id.baseweb_webview);
- mWebView.getSettings().setJavaScriptEnabled(true);
- mWebView.setWebViewClient(new WebViewClient());
配置默认浏览器
在布局中使用WebView控件,用于加载网页,一个简单例子:
布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <WebView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/webview"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
控件加载网页
- WebView myWebView = (WebView) findViewById(R.id.webview);
- myWebView.loadUrl("http://qgjie123.com");
配置AndroidManifest.xml网络权限
- <uses-permission android:name="android.permission.INTERNET" />
配置默认WebView
- myWebView.setWebViewClient(new WebViewClient());
使用WebView加载qgjie123.com,baidu.com等网页时,设置setWebViewClient()后,点击baidu.com连接页面,使得当前连接在WebView中打开。
配置支持JavaScript
- myWebView.getSettings().setJavaScriptEnabled(true);
如果加载的网页包含JavaScript脚本,设置setJavaScriptEnable()为true,默认不支持脚本,
其他
WebView加载网页的三种方式:
第一种:指定网址
- myWebView.loadUrl("http://qgjie123.com");
第二种:指定网页
- myWebView.loadUrl("file:///android_asset/html/ys_android.html");
- mWebView.loadUrl("file:///android_asset/html/falv_android.html");
ys_android.html和falv_android.html是存放在asset文件夹中的网页,上面是固定的写法
第三种:HTML字符串代码
- String htmlStr="<html><body><h2>Hellow World!</h2></body></html>"
- myWebView.loadData(htmlStr, "text/html", "utf-8");
你可能感兴趣的文章
来源:TeachCourse,
每周一次,深入学习Android教程,关注(QQ158#9359$239或公众号TeachCourse)
转载请注明出处: https://www.teachcourse.cn/1226.html ,谢谢支持!
转载请注明出处: https://www.teachcourse.cn/1226.html ,谢谢支持!