android 2.2:将参数传递给js文件中的javascript函数并返回值

时间:2012-04-23 08:00:53

标签: javascript android webview android-webview

我在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">&#64397;</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">&#64337;&#64338;&#64339;&#64340;&#64341;</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>
                                 &#64342;&#64343;&#64344;&#64345;&#64346;</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>
                                 &#64347;&#64348;&#64349;</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">&#64350;&#64351;&#64352;&#64353;</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>
                                 &#64354;&#64355;&#64356;&#64357;&#64358;</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">&#64359;<br>
                                 &#64360;&#64361;&#64362;</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">&#64363;&#64364;&#64365;<br>
                                 &#64366;&#64367;&#64368;&#64369;<br>

                                 &#64370;&#64371;&#64372;</a></span>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

0 个答案:

没有答案