现在的位置: 首页 > 综合 > 正文

本地程序与JavaScript互操作

2017年12月15日 ⁄ 综合 ⁄ 共 4699字 ⁄ 字号 评论关闭

 

在assets下新建html文件夹,在html文件夹中新建show_js.html和

delete_js.html,在res下存放一名为pic_m的图片。

 

 

在show_js.html中:

 

<head>

  <title>我的Android博客</title>

  <meta http-equiv="Content-Type" content="text/html;charset=GBK">

  <script language="javascript">

     function openAlert(){

       window.alert("我的Android博客\nblog.sina.com.cn/u/2853310902") ;

     }

     function openConfirm(){

       if (window.confirm("是否删除此信息?")) {

         window.location = "delete_js.html" ;

       }

     }

  </script>

</head>

<input type="button" value="弹出警告框" onclick="openAlert()">

<input type="button" value="弹出确认框" onclick="openConfirm()">

 

 

 

 

 

在delete_js.html中:

 

<head>

  <title>魔乐科技软件学院:www.mldnjava.cn</title>

  <meta http-equiv="Content-Type" content="text/html;charset=GBK">

</head>

<h1>信息已删除!</h1>

 

 

 

 

在main.xml中:

 

<?xml version="1.0" encoding="utf-8"?>

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

 

 

 

 

 

 

在MyWebViewDemo.java中:

 

package com.li.webview;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.app.Dialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.webkit.JsResult;

import android.webkit.WebChromeClient;

import android.webkit.WebView;

import android.widget.Toast;

 

public class MyWebViewDemo extends Activity {

  private WebView webview = null;

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.webview = (WebView) super.findViewById(R.id.webview);

     this.webview.getSettings().setJavaScriptEnabled(true); // 启用JavaScript

     this.webview.getSettings().setBuiltInZoomControls(true); // 控制页面缩放

     this.webview.setWebChromeClient(new WebChromeClientImpl());

    this.webview.loadUrl("file:/android_asset/html/show_js.html");

  }

 

  private class WebChromeClientImpl extends WebChromeClient {

 

     @Override

     public boolean onJsAlert(WebView view, String url, String message,

         final JsResult result) {

       Dialog dialog = new AlertDialog.Builder(MyWebViewDemo.this)

            .setIcon(R.drawable.pic_m)

            .setTitle("MLDN警告")

            .setMessage(message)

            .setPositiveButton(android.R.string.ok,

                new DialogInterface.OnClickListener() {

 

                   public void onClick(DialogInterface dialog,

                       int which) {

                     Toast.makeText(MyWebViewDemo.this, "关闭警告框",

                          Toast.LENGTH_SHORT).show();

                     result.cancel();

                   }

                }).create();

       dialog.show() ;

       return true;

     }

 

     @Override

     public boolean onJsConfirm(WebView view, String url, String message,

         final JsResult result) {

       Dialog dialog = new AlertDialog.Builder(MyWebViewDemo.this)

            .setIcon(R.drawable.pic_m)

            .setTitle("确定删除?")

           .setMessage(message)

            .setPositiveButton("删除",

                new DialogInterface.OnClickListener() {

 

                   public void onClick(DialogInterface dialog,

                       int which) {

                     Toast.makeText(MyWebViewDemo.this, "确定删除",

                          Toast.LENGTH_SHORT).show();

                     result.confirm();

                   }

                })

            .setNegativeButton("取消",

                new DialogInterface.OnClickListener() {

                   public void onClick(DialogInterface dialog,

                       int which) {

                     Toast.makeText(MyWebViewDemo.this, "取消删除",

                          Toast.LENGTH_SHORT).show();

                     result.cancel();

                   }

                }).create();

       dialog.show() ;

       return true;

     }

 

     @Override

     public void onReceivedTitle(WebView view, String title) {

       MyWebViewDemo.this.setTitle(title) ;

       super.onReceivedTitle(view, title) ;

     }

 

  }

}

 

 

 

 

 

 

在AndroidManifest.xml中修改权限:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.webview"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

  <uses-permission android:name="android.permission.INTERNET" />

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyWebViewDemo"

            android:label="@string/title_activity_my_web_view_demo">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

抱歉!评论已关闭.