Android---webview的用法
来源:互联网 发布:喜羊羊在法国禁播 知乎 编辑:程序博客网 时间:2024/06/05 03:30
基本代码:
Uri uri = Uri.parse("http://www.baidu.com");
Intent intent = new Intent(this,uri);
startActivity(intent);
1.将webview加入你的应用
<WebView
android:id = "@+id/WebView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
2.使用webview加载页面
web资源用:webview.loadUrl("http://www.baidu.com");
本地文件用:webview.loadUr("file:///android_asset/XX.html);
webview.requestFocus();//是页面获得焦点
3.Mainfest.xml
<uses-permission andriod:name = "android.permission.INTERNET"/>
4.处理页面导航
//覆盖webview默认通过第三方或者是系统浏览器打开网页的行为,是的网页 可以在
webview中打开
webview。loadUrl("http://www.baidu.com");
webview.setWebviewClient(new WebViewClient(){
@override
public boolean shouldOverriderUrlLoading(webview view,String url){
//返回值是true的时候控制网页在webview中打开,false的时候需要用第三方
浏览器打开
view.loadUri(url);
return true;
}
//WebViewClient帮助WebView去处理一些页面控制和请求通知
});
//启用支持JavaScript
WebSettings settings = webView.getSettings();
settings.setJavaScripteEnabled(true);
}
5.后退与前进
goBack()和goFore();
//改写物理按键---返回的逻辑
@Override
publiv boolean onKeyDown(int keyCode,KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK){
if(webview.canGoBack()){
webview.goBack();
return true;
}else{
system.exit(0);//退出程序
}
}
}
6.判断页面加载过程
private ProgressDialog dialog;
webView.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(Webview view,int newProgress){
if(newProgress == 100){
closeDialoag();
}else{
openDialog();
}
}
private void closeDialog(){
if(dialog!=null&&dialog.isEmpty(0){
dialog.dismiss();
}
}
private void openDialog(){
if(dialog == null){
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("正在加载");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(newProgress);
dialog.show()
}else{
dialog.setProgress(newProgress);//刷新当前的进度
}
}
});
7.webview缓存的运用
//webview加载页面优先使用缓存加载
setting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
0 0
- Android---webview的用法
- 关于Android webView的用法
- android 中WebView的用法
- Android中WebView的用法
- android中WebView的用法
- Android WebView的基本用法
- android webView 的常见问题与高级用法
- Android WebView的用法示例代码
- Android中WebView的意义及用法
- android学习十九(WebView的用法)
- android中WebView的用法总结
- Android WebView的用法示例代码
- android webView用法
- android webView用法
- Android---WebView基本用法
- android webview用法小结
- Android WebView 基本用法
- Android WebView用法
- [LeetCode]Basic Calculator II
- zoj2099
- Windows无法连接到选定网络,网络可能不在区域中。请刷新可用网络的列表,重新尝试
- POJ 3984 迷宫问题 BFS
- 事件格式化JQuery可以说是Web项目中不可或缺的一个Javascript类库 js格式化时间的方法。
- Android---webview的用法
- 深入剖析Android四大组件(一)——Activity生命周期详解
- Android错误之--ActivityManager: Error: Activity class {com.borrowbook.activity
- DNS子域授权及视图view功能的应用
- 深入了解View(三)—— draw绘制流程分析
- 解决Word文档邮件合并产生的超长小数位
- android图片处理之图像模糊
- 己亥岁
- Android错误之-- ActivityManager: Error: Activity class.....does not exist.