zoukankan      html  css  js  c++  java
  • [Android_Webview]WebView的基本用法

     1、WebView 显示进度条 在onCreate事件里写:

    1. WebView myWebView = (WebView) findViewById(R.id.webView1);
    2. final Activity activity = this;
    3. myWebView.setWebChromeClient(new WebChromeClient() {
    4.            public void onProgressChanged(WebView view, int progress) {
    5.                 activity.setTitle("正在努力为您加载....");
    6.                 activity.setProgress(progress * 100);
    7.                 if(progress == 100)
    8.                     activity.setTitle("已完成");//或者设置为原有的Activity标题
    9.           }
    10.        });

    2、网页自适应webView宽度 在onCreate事件里写:

    1. WebView myWebView = (WebView) findViewById(R.id.webView1);
    2. myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

    3、webView支持网页JavaScript语言 在onCreate事件里写:

    1. WebView myWebView = (WebView) findViewById(R.id.webView1);
    2. myWebView.getSettings().setJavaScriptEnabled(true);

    4、网页里面超链接在webview里加载而不是在手机默认浏览器打开 在onCreate事件里写:

    1. WebView myWebView = (WebView) findViewById(R.id.webView1);
    1. myWebView.setWebViewClient(new WebViewClient(){
    2.        public boolean shouldOverrideUrlLoading(WebView view, String url) {
    3.              view.loadUrl(url);
    4.               return true;
    5.           }
    6.       });
    1. myWebView.loadUrl("http://www.baidu.com"));//默认加载url只能放在setWebViewClient后面 否则网页里面超链接还是在手机默认浏览器打开而不是在WebView里加载

    5、按手机返回按钮后退网页而不是打开WebView程序之前的界面:

    1. public boolean onKeyDown(int keyCode, KeyEvent event)
    2.    { // Check if the key event was the BACK key and if there's history
    3.     WebView myWebView = (WebView) findViewById(R.id.webView1);
    4.     if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
    5.         {
    6.             myWebView.goBack();
    7.             return true;
    8.         } // If it wasn't the BACK key or there's no web page history, bubble up to the default // system behavior (probably exit the activity)
    9.     return super.onKeyDown(keyCode, event);
    10.    }

    6、网页Js调用Android程序:

    新建一类JavaScriptInterface

    1. package com.example.androidhouse;
    2. import android.content.Context;
    3. import android.widget.Toast;
    4. public class JavaScriptInterface {
    5.     Context mContext;
    6.     /** Instantiate the interface and set the context */
    7.     JavaScriptInterface(Context c) { mContext = c; }
    8.     /** Show a toast from the web page */
    9.     public void showToast(String toast)
    10.     {
    11.         Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    12.     }
    13. }

    在onCreate事件里写:

    1. WebView myWebView = (WebView) findViewById(R.id.webView1);
    1. myWebView.addJavascriptInterface(new JavaScriptInterface(this),"Android");

    网页js代码里写:

    [javascript] view plaincopy
    1. window.Andorid.showToast("我调用的Android程序");

    切记:在Andorid2.2 和Android2.3里面 webview的js调用会引起系统崩溃出现vm aborting错误 这是这两个版本的BUG 解决起来很麻烦 到现在为止,我仍未找到解决的办法。

  • 相关阅读:
    Did not find handler method for springMVC资源文件扫描不到---关于spring的那些坑
    mysql中OPTIMIZE TABLE的作用
    Linux环境下apache性能测试工具ab使用详解
    sqlite数据库 adb 从配置到查询表中数据全过程-----献给初学的自己
    c3p0参数解释
    linux下如何启动/停止/重启mysql:
    [MySQL] 变量(参数)的查看和设置
    mysql运行参数详解
    单例模式 理解,简单通透
    this的一些场景
  • 原文地址:https://www.cnblogs.com/webapplee/p/3771940.html
Copyright © 2011-2022 走看看