android学习笔记之webview的简单使用
来源:互联网 发布:处方点评软件 编辑:程序博客网 时间:2024/05/17 09:06
package com.example.administrator.myexercise;import android.annotation.SuppressLint;import android.app.Activity;import android.app.ProgressDialog;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.KeyEvent;import android.webkit.WebChromeClient;import android.webkit.WebResourceRequest;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;/** * Created by Administrator on 2016/6/28 0028. */public class WebviewActivity extends Activity{ private String url = "http://www.hao123.com"; private WebView webView; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview); initViews(); /* Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW,uri); startActivity(intent);*/ } @SuppressLint("WrongViewCast") private void initViews(){ webView = (WebView) findViewById(R.id.my_webview); //webview加载本地资源 //webView.loadUrl("file:///android_asset/qilai.html"); //webview加载外部资源 webView.loadUrl(url); //覆盖Webview默认通过第三方或者系统浏览器打开网页的行为,使得网页可以在浏览器中打开 webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { //返回值为true的时候控制网页在webview中打开,为false的时候调用浏览器打开 return true; } //WebviewClient帮助webview去处理一些页面控制和请求通知 }); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); //设置优先使用缓存 webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //设置加载进度 webView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { if(newProgress == 100){ //加载完成 closeDialog(); }else{ //正在加载 openDialog(newProgress); } //super.onProgressChanged(view, newProgress); } }); } /** * 打开对话框 * @param newProgress 加载的进度 */ private void openDialog(int newProgress){ if (dialog == null){ dialog = new ProgressDialog(WebviewActivity.this); dialog.setTitle("正在加载"); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(newProgress); dialog.show(); }else{ dialog.setProgress(newProgress); } } /** * 关闭对话框 */ private void closeDialog(){ if (dialog != null && dialog.isShowing()){ dialog.dismiss(); dialog = null; } } /** * 改写物理按键返回的逻辑 * @param keyCode * @param event * @return */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){ webView.goBack(); return true; } return super.onKeyDown(keyCode, event); }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/my_webview" android:layout_width="match_parent" android:layout_height="match_parent"></WebView></LinearLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.administrator.myexercise"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".WebviewActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
0 0
- android学习笔记之webview的简单使用
- Android学习之笔记---WebView的使用
- android 学习笔记 webView的简单调用
- Android学习笔记之五-WebView的使用
- Android之WebView的简单使用
- Android开发学习笔记:WebView的使用
- android学习笔记之WebView
- WebView的简单使用——学习笔记
- 【Android】安卓学习笔记之使用WebView加载网页
- Android WebView的简单使用
- Android WebView的简单使用
- Android WebView的简单使用
- Android WebView的简单使用
- Android之WebView的使用与简单浏览器
- Android学习笔记之ListView的简单使用
- android学习笔记之SQLite的简单使用
- android学习笔记之SharePreferences简单使用
- android 学习笔记3——WebView的使用
- Linux内核驱动之spi子系统(二)用户空间spi驱动
- 从github下载某个git库的4种方法
- HTML 简易计算器的制作
- unity 图片资源导入优化
- SQLite简单创建
- android学习笔记之webview的简单使用
- sim900A透传调试及问题解决
- java IO总结
- 跨平台app开发中的点击更新数据实例
- 不变对象(Immutable Objects)
- 配置VIM-将VIM打造成为强大的IDE
- 导入现有项目到eclipse出现的编译问题
- Android之AsyncTask的使用(二)之文件下载
- ios9使用自签名ssl -9824 -9801