zoukankan      html  css  js  c++  java
  • 使用WebView显示网页

    简单的页面跳转

    package com.example.webtest;

    import java.security.PublicKey;

    import android.support.v7.app.ActionBarActivity;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class MainActivity extends Activity {
    private String url = "http://2014.qq.com/";
    private ProgressDialog progressDialog;
    private WebView web;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web);
    web = (WebView) findViewById(R.id.web);
    // Uri uri = Uri.parse(url);
    // URL:统一资源定位符 也就是网址 例如 http://www.microsoft.com/
    // URI:通用资源标志符 http://www.acme.com/support/suppliers.htm
    // Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    // startActivity(intent);
    init();

    /*
    * Uri uri = Uri.parse(url); // url为你要的链接 Intent intent = new
    * Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
    */

    }

    private void init() {
    // TODO Auto-generated method stub

    // Log.d("tag", "DAYIN");
    web.loadUrl(url);
    // web.loadUrl("file:///android_asset/1.html");
    web.setWebViewClient(new WebViewClient() {

    // 覆盖webView默认通过第三方或者是系统浏览器打开网页的行为,使得网页可以在WebView中打开
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub
    view.loadUrl(url);

    // 返回值是TRUE的时候控制网页在webview中打开,如果为false的时候则调用系统浏览器或者第三方浏览器打开
    return super.shouldOverrideUrlLoading(view, url);
    }
    });

    // Webviewclient帮助webview去处理一些页面控制和请求通知。
    WebSettings settings = web.getSettings();
    settings.setJavaScriptEnabled(true);// 使得手机自动适应网页

    settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // webvie加载页面优先使用缓存加载
    web.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
    // TODO Auto-generated method stub
    super.onProgressChanged(view, newProgress);

    if (newProgress == 100) {
    // 网页加载完毕, 关闭progressDialog
    closeDialog();
    } else {
    openDialog(newProgress);
    }

    }

    private void openDialog(int newProgress) {
    // TODO Auto-generated method stub

    if (progressDialog == null) {
    progressDialog = new ProgressDialog(MainActivity.this);
    progressDialog.setTitle("正在加载中");
    progressDialog
    .setProgressStyle(progressDialog.STYLE_HORIZONTAL);
    progressDialog.setProgress(newProgress);
    progressDialog.show();

    } else {
    progressDialog.setProgress(newProgress);// 显示新的进度
    }
    }

    private void closeDialog() {
    // TODO Auto-generated method stub
    if (progressDialog != null && progressDialog.isShowing()) {
    progressDialog.dismiss();// 取消显示
    progressDialog = null;

    }
    }
    });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

    // 改写物理按键--返回的逻辑
    // TODO Auto-generated method stub
    if (keyCode == KeyEvent.KEYCODE_BACK) {
    if (web.canGoBack()) {
    web.goBack();
    return true;// 在javaScript里,return有终止函数的执行和传递数值,两种功能。
    // return false 就是返回假值,从而终止了函数的执行
    // return true 就是返回真值,从而传递数值,继续执行函数
    } else {
    System.exit(0);
    }

    }
    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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
    return true;
    }
    return super.onOptionsItemSelected(item);
    }
    }

    web.xml文件

    <?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"
        android:orientation="vertical" >
        <WebView 
            android:id="@+id/web"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </LinearLayout>
  • 相关阅读:
    OGG-01168
    浅谈webuploader上传文件
    [ IE浏览器兼容问题 ] Web Uploader 在IE、FireFox下点击上传没反应
    webuploader在ie7下的flash模式的使用
    记一次项目使用webuploader爬坑之旅
    解决Web Uploader上传文件和图片 延迟和not defined
    SpringMVC上传图片总结(2)--- 使用百度webuploader上传组件进行上传图片
    百度上传插件(webupload)单文件(单图片)上传设置
    webservice大文件怎么传输
    java使用WebUploader做大文件的分块和断点续传
  • 原文地址:https://www.cnblogs.com/yi-mi-yangguang/p/5851005.html
Copyright © 2011-2022 走看看