我在android 2.2中使用webView
我想将参数传递给js文件中的javascript函数,
我想要返回值到我的机器人
我的代码中有什么问题?
我的代码:
WebView webView1;
MyJavaScriptInterface myJavaScriptInterface1;
public void onCreate(Bundle savedInstanceState) {
webView1 = (WebView) findViewById(R.id.webView1);
myJavaScriptInterface1 = new MyJavaScriptInterface(this);
webView1.addJavascriptInterface(myJavaScriptInterface1,"AndroidFunction");
EnableJavaScriptAndMultitouch(webView1);
webView1.loadUrl("file:///android_asset/P_001.html");
}
public void EnableJavaScriptAndMultitouch(WebView mWebView) {
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
}
public void onLongPress(MotionEvent e) {
webView1.loadUrl("javascript:SelectAyaInPosition("
+ String.valueOf(e.getX()) + "," + String.valueOf(e.getY())
+ ", 'black', 'red'");
}
////////////////////////////////
public class MyJavaScriptInterface {
Context mContext;
MyJavaScriptInterface(Context c) {
mContext = c;
}
public void getElementID(String ID) {
Toast.makeText(mContext, ID, Toast.LENGTH_SHORT).show();
}
}
my js文件在assets文件夹中,带有我的html文件
function ClearAll(defaultColor)
{
var allLinks = document.all.tags('a');
for(var i = 0; i < allLinks.length; i++)
allLinks[i].style.color = defaultColor;
}
function ColorAya(aya, sura, defaultColor, highlightColor)
{
ClearAll(defaultColor);
var elementSelected = document.getElementById('a' + sura + '_' + aya);
elementSelected.style.color= highlightColor;
}
function GetItemInPosition(x, y)
{
var elementSelected = document.elementFromPoint(x, y);
return elementSelected.id;
}
function SelectAyaInPosition(x, y, defaultColor, highlightColor)
{
ClearAll(defaultColor);
var elementSelected = document.elementFromPoint(x, y);
elementSelected.style.color= highlightColor;
showAndroidToast(elementSelected.id);
}
function showAndroidToast(toast) {
AndroidFunction.getElementID(toast);
}
我的HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta name='viewport' content=
'width=device-width; initial-scale=1.0; maximum-scale=20.0; user-scalable=1; minimum-scale=1.0; '>
<link rel='stylesheet' charset='utf-8' type='text/css'
href='Main.css'>
<style type="text/css">
@font-face
{
font-family: AQF_P001_HA;
src: url('file:///android_asset/fonts/AQF_P001_HA.ttf');
}
.sc_F0
{
font-family: AQF_P001_HA;
} @font-face
{
font-family: AQF_BSML;
src: url('file:///android_asset/fonts/AQF_BSML.ttf');
}
.sc_F0
{
font-family: AQF_BSML;
}
</style>
<script type='text/javascript' language='javascript' src='JSColorAya.js'>
</script>
<title></title>
</head>
<body id="body">
<img alt="" src="MainPagesFram@2x.png" class="imgFrame">
<div style="position:absolute;top:18px;right:127px;font-family: AQF_BSML; mso-font-charset: 0;font-size:15pt">ﮍ</div>
<table onclick="" style=
"position: absolute; width: 100%; height: 100px%; z-index: 0; left: 0px; top: 110px; direction: rtl; overflow: visible; line-height: 30px;"
id="Table00" name="Table00">
<tbody>
<tr align="justify" dir="rtl">
<td align="middle">
<table border="0" cellspacing="0"
cellpadding="0" align="center">
<tr>
<td align="center"><span class=
"ayaColor" style="font-size:17pt"><a name="1_1"></a> <a
id="a1_1" style=
"cursor: pointer; direction: rtl; line-height: 165%; font-family: AQF_P001_HA; mso-font-charset: 0"
target=
"_top">ﭑﭒﭓﭔﭕ</a><a
name="1_2"></a><a id="a1_2" style=
"cursor: pointer; direction: rtl; line-height: 165%; font-family: AQF_P001_HA; mso-font-charset: 0"
target="_top"><br>
ﭖﭗﭘﭙﭚ</a><a
name="1_3"></a><a id="a1_3" style=
"cursor: pointer; direction: rtl; line-height: 165%; font-family: AQF_P001_HA; mso-font-charset: 0"
target="_top"><br>
ﭛﭜﭝ</a><a
name="1_4"></a><a id="a1_4" style=
"cursor: pointer; direction: rtl; line-height: 165%; font-family: AQF_P001_HA; mso-font-charset: 0"
target=
"_top">ﭞﭟﭠﭡ</a><a
name="1_5"></a><a id="a1_5" style=
"cursor: pointer; direction: rtl; line-height: 165%; font-family: AQF_P001_HA; mso-font-charset: 0"
target="_top"><br>
ﭢﭣﭤﭥﭦ</a><a
name="1_6"></a><a id="a1_6" style=
"cursor: pointer; direction: rtl; line-height: 165%; font-family: AQF_P001_HA; mso-font-charset: 0"
target="_top">ﭧ<br>
ﭨﭩﭪ</a><a
name="1_7"></a><a id="a1_7" style=
"cursor: pointer; direction: rtl; line-height: 165%; font-family: AQF_P001_HA; mso-font-charset: 0"
target=
"_top">ﭫﭬﭭ<br>
ﭮﭯﭰﭱ<br>
ﭲﭳﭴ</a></span>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>