分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

10.3.3 WebView的几个常见功能

发布时间:2023-09-06 01:12责任编辑:熊小新关键词:Web

当前主流的开发模式是“WebView+ProgressDialog”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ???android:orientation="vertical" ???android:layout_width="fill_parent" ???android:layout_height="fill_parent" > ???<WebView ???????android:id="@+id/webview" ???????android:layout_width="fill_parent" ???????android:layout_height="fill_parent"/></LinearLayout>
package com.example.progresstest;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.view.KeyEvent;import android.view.Menu;import android.view.View;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.ProgressBar;import android.widget.Toast;public class WebViewActivity extends Activity { ???private ProgressDialog progressBar; ???private WebView webView; ???private AlertDialog alertDialog; ???@Override ???protected void onCreate(Bundle savedInstanceState) { ???????super.onCreate(savedInstanceState); ???????setContentView(R.layout.webview); ???????//加载WebView ???????initWebView(); ???} ???private void initWebView() { ???????progressBar = ProgressDialog.show(WebViewActivity.this, null, "正在进入网页,请稍后...."); ???????//获得WebView组件 ???????webView = (WebView) this.findViewById(R.id.webview); ???????webView.getSettings().setJavaScriptEnabled(true); ???????webView.loadUrl("http://www.google.com"); ???????alertDialog = new AlertDialog.Builder(this).create(); ???????//设置视图客户端 ???????webView.setWebViewClient(new MyWebViewCLient()); ???} ??@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) { ???// TODO Auto-generated method stub ??????if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){ ??????????webView.goBack(); ??????????return true; ??????} ???return super.onKeyDown(keyCode, event);}/* ???@Override ???public boolean onCreateOptionsMenu(Menu menu) { ???????// Inflate the menu; this adds items to the action bar if it is present. ???????getMenuInflater().inflate(R.menu.main, menu); ???????return true; ???}*/ ???class MyWebViewCLient extends WebViewClient { ????????????@Override ???????????public boolean shouldOverrideUrlLoading(WebView view, String url) { ???????????????// TODO Auto-generated method stub ????????????????view.loadUrl(url); ???????????????return true; ???????????} ????????????@Override ???????????public void onPageFinished(WebView view, String url) { ???????????// TODO Auto-generated method stub ??????????????if(progressBar.isShowing()){ ??????????????????progressBar.dismiss(); ??????????????} ???????????} ????????????@Override ???????????public void onReceivedError(WebView view, int errorCode, ???????????String description, String failingUrl) { ???????????// TODO Auto-generated method stub ???????????Toast.makeText(WebViewActivity.this, "网页加载出错! ?", Toast.LENGTH_LONG); ???????????alertDialog.setTitle("ERROR"); ???????????alertDialog.setMessage(description); ???????????alertDialog.setButton("OK", new DialogInterface.OnClickListener() { ???????????????????????????????@Override ???????????????public void onClick(DialogInterface dialog, int which) { ???????????????????// TODO Auto-generated method stub ???????????????????????????????????} ???????????}); ???????????alertDialog.show(); ????????????} ???}}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" ???package="com.example.progresstest" ???android:versionCode="1" ???android:versionName="1.0" > ???<uses-sdk ???????android:minSdkVersion="8" ???????android:targetSdkVersion="17" /> ???<application ???????android:allowBackup="true" ???????android:icon="@drawable/ic_launcher" ???????android:label="@string/app_name" ???????android:theme="@style/AppTheme" > ???????<activity ???????????android:name="com.example.progresstest.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><uses-permission android:name="android.permission.INTERNET"/></manifest>

10.3.3 WebView的几个常见功能

原文地址:http://www.cnblogs.com/ZHONGZHENHUA/p/7559400.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved