App ==WebView==>asset ===>Javascript
------------------------------------->
1.先載入網頁 wv1.loadUrl(url);
2.再傳送訊息參數
wv1.loadUrl("javascript:fun1(83,5)");
3.由Android之AlertDialog送出訊息
package com.example.theoryproject1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
public class Webhomefile2
extends Activity implements android.view.View.OnClickListener
{
private WebView wv1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.webhomwfile2);
wv1=(WebView)findViewById(R.id.webView1);
WebSettings wset=wv1.getSettings();
wset.setJavaScriptEnabled(true);
wset.setBuiltInZoomControls(true);
wv1.loadUrl("file:///android_asset/hopealert12.html");
WebChromeClient wcc=new WebChromeClient()
{
public boolean shouldOverrideUrlLoading(WebView view,String url)
{
view.loadUrl(url);
return true;
}
@Override
public boolean onJsAlert(WebView view, String url, String message,
final JsResult result) {
// TODO Auto-generated method stub
AlertDialog.Builder ad =new AlertDialog.Builder(Webhomefile2.this);
ad.setTitle("輸出對話方塊");
ad.setMessage(message);
ad.setPositiveButton("確定",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
result.confirm();
}
});
ad.show(); ;
return true;
}
@Override
public boolean onJsConfirm(WebView view, String url,
String message, JsResult result) {
// TODO Auto-generated method stub
return super.onJsConfirm(view, url, message, result);
}
@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue,JsPromptResult result) {
// TODO Auto-generated method stub
return super.onJsPrompt(view, url, message, defaultValue, result);
}
};
wv1.setWebChromeClient(wcc);
Button bt1=(Button)findViewById(R.id.button1);
bt1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wv1.loadUrl("javascript:fun1(83,5)");
}
}
----------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
</head>
<script language="javascript">
function fun1(a,b)
{
var total=a+b;
alert("a+b="+total);
}
</script>
<body >
<a href="Javascript:void();" onclick="fun1()">啟動輸出對話方塊</a>
</body>
</html>
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
public class Webhomefile2
extends Activity implements android.view.View.OnClickListener
{
private WebView wv1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.webhomwfile2);
wv1=(WebView)findViewById(R.id.webView1);
WebSettings wset=wv1.getSettings();
wset.setJavaScriptEnabled(true);
wset.setBuiltInZoomControls(true);
wv1.loadUrl("file:///android_asset/hopealert12.html");
WebChromeClient wcc=new WebChromeClient()
{
public boolean shouldOverrideUrlLoading(WebView view,String url)
{
view.loadUrl(url);
return true;
}
@Override
public boolean onJsAlert(WebView view, String url, String message,
final JsResult result) {
// TODO Auto-generated method stub
AlertDialog.Builder ad =new AlertDialog.Builder(Webhomefile2.this);
ad.setTitle("輸出對話方塊");
ad.setMessage(message);
ad.setPositiveButton("確定",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
result.confirm();
}
});
ad.show(); ;
return true;
}
@Override
public boolean onJsConfirm(WebView view, String url,
String message, JsResult result) {
// TODO Auto-generated method stub
return super.onJsConfirm(view, url, message, result);
}
@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue,JsPromptResult result) {
// TODO Auto-generated method stub
return super.onJsPrompt(view, url, message, defaultValue, result);
}
};
wv1.setWebChromeClient(wcc);
Button bt1=(Button)findViewById(R.id.button1);
bt1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wv1.loadUrl("javascript:fun1(83,5)");
}
}
----------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
</head>
<script language="javascript">
function fun1(a,b)
{
var total=a+b;
alert("a+b="+total);
}
</script>
<body >
<a href="Javascript:void();" onclick="fun1()">啟動輸出對話方塊</a>
</body>
</html>
沒有留言:
張貼留言