【Android基础知识】WebView基本用法
来源:互联网 发布:linux trap 编辑:程序博客网 时间:2024/06/06 03:44
Android WebView用来加载网页,显示网页内容。
1.在布局文件中添加WebView组件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.webviewdemo.MainActivity" > <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /></RelativeLayout>2.在AndroidManifest.xml文件中添加访问互联网的权限
<uses-permission android:name="android.permission.INTERNET"/>3.使用WebView
public class MainActivity extends Activity {private String uri = "http://www.baidu.com";private WebView webView;private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* //通过intent跳转 Uri mUri = Uri.parse(uri); Intent intent = new Intent(Intent.ACTION_VIEW,mUri); startActivity(intent);*/ init(); } private void init(){ webView = (WebView)findViewById(R.id.webView1); /*//webView加载本地资源 webView.loadUrl("file:///android_asset/t.html");*/ //webView加载外部资源 webView.loadUrl(uri); //覆盖WebView默认通过第三方或者是浏览器打开网页的行为,使得网页可以在WebView中打开,需要internet权限 webView.setWebViewClient(new WebViewClient(){@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {//返回值是true的时候控制网页在WebView中去打开,false则是在第三方打开view.loadUrl(url);return true;//return super.shouldOverrideUrlLoading(view, url);} }); //启用支持JavaScript WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); //WebView 加载页面优先使用缓存加载 settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //判断页面的加载过程,进度 webView.setWebChromeClient(new WebChromeClient(){@Overridepublic void onProgressChanged(WebView view, int newProgress) {//newProgress 1-100之间的整数if(newProgress == 100){//网页加载完毕closeDialog();}else{//网页正在加载,打开ProgressDialogopenDialog(newProgress);}} private void closeDialog(){ if(progressDialog != null && progressDialog.isShowing()){ progressDialog.dismiss(); progressDialog = null; } } private void openDialog(int progress){ if(progressDialog == null){ progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("正在加载"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setProgress(progress); progressDialog.show(); }else{ progressDialog.setProgress(progress); progressDialog.show(); } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK){ Toast.makeText(this, "back", Toast.LENGTH_SHORT).show(); if(webView.canGoBack()){ webView.goBack(); return true; }else{ System.exit(0); } } return super.onKeyDown(keyCode, event); }}
0 0
- 【Android基础知识】WebView基本用法
- Android---WebView基本用法
- Android WebView 基本用法
- Android WebView的基本用法
- WebView基本用法
- WebView基本用法
- [Android基础知识] WebView详解
- Android webview基础知识
- android WebView, WebChromeClient和WebViewClient加载网页基本用法
- android WebView, WebChromeClient跟WebViewClient加载网页基本用法
- android WebView, WebChromeClient和WebViewClient加载网页基本用法
- Android基础—WebView(网页视图)基本用法
- webview在android端基本用法(三)
- Android:WebView基本使用
- Android-WebView基本使用
- Android WebView基本使用
- WebView(网页视图)基本用法
- android webView用法
- Linux中Samba详细安装
- 程序员常常登录的技术网站
- Serializable中serialVersionUID的作用
- 通过几个Hello World感受.NET Core全新的开发体验
- Codeforces Round #360 (Div. 1) B. Remainders Game(中国剩余定理)
- 【Android基础知识】WebView基本用法
- SQL语句中AND OR运算符优先级
- bootstrap-datetimepicker时间控件
- Ubuntu下github pages+hexo搭建自己的博客
- spring java 发送邮箱验证
- small使用
- Parallels 10(v10.40) ubuntu desktop 14.04.4 无法安装Parallels tools
- solr原理详解
- [从头读历史] 第287节 神之物语 赫拉克勒斯的故事