如何从android调用Javascript代码

时间:2015-12-17 14:07:41

标签: javascript android html

我想调用下面给出的JavaScript代码,并希望将一些参数发送到该代码,并根据我希望该脚本的响应。

<script src="https://secure.ewaypayments.com/
    scripts/eCrypt.js"
    class="eway-paynow-button"
    data-publicapikey="XXX-XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
    data-amount="1000"
    data-currency="NZD" > 
</script>

3 个答案:

答案 0 :(得分:0)

在Android中,您只需从WebView调用JavaScript,如:

yourWebView.loadUrl("javascript:testEcho('Hello World!')");

答案 1 :(得分:0)

不知道为什么要这样做,但您可以在活动中嵌入WebView。从上面的外观来看,代码会创建一个付款按钮。

所以:
1.制作你的活动布局,并将你的webview放在按钮所在的任何地方。
2.创建一个HTML文件,将代码放在上面。将HTML文件放在资源文件夹中 3.在您的活动中,将html文件加载到您的webview中:

#include <iostream>    
#include <string>    
using namespace std;

int main() {

    string s="ABCD";
    for(int i=0; i<s.length(); i++) {
        for(int j=i; j<s.length(); j++) {
            string s1(s,i,j+1);
            cout<<s1<<endl;
        }
    }
    return 0;
}

我刚刚意识到你想从应用程序传递参数。您可以这样做的一种方法是创建一个HTML字符串,而不是HTML文件,您可以在其中放置所需的信息。所以......

    WebView webView = (WebView)findViewById(R.id.webView1);
    webView.loadUrl("file:///android_asset/file.html");
    webView.getSettings().setJavaScriptEnabled(true);

答案 2 :(得分:0)

布局

<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content" >    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/webkit"        android:layout_width="fill_parent"        android:layout_height="fill_parent" />

Java代码

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class AndroidJSWebView extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //WebView Object
    WebView browser;
    browser=(WebView)findViewById(R.id.webkit);
    //Enable Javascript
    browser.getSettings().setJavaScriptEnabled(true);
    //Inject WebAppInterface methods into Web page by having Interface name 'Android' 
    browser.addJavascriptInterface(new WebAppInterface(this), "Android");
    //Load URL inside WebView
    browser.loadUrl("https://www.google.co.in/");
}
//Class to be injected in Web page
public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /**
     * Show Toast Message
     * @param toast
     */
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }

    /**
     * Show Dialog 
     * @param dialogMsg
     */
    public void showDialog(String dialogMsg){
        AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();

        // Setting Dialog Title
        alertDialog.setTitle("JS triggered Dialog");

        // Setting Dialog Message
        alertDialog.setMessage(dialogMsg);

        // Setting alert dialog icon
        //alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(mContext, "Dialog dismissed!", Toast.LENGTH_SHORT).show();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

    /**
     * Intent - Move to next screen
     */
    public void moveToNextScreen(){
         AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
         // Setting Dialog Title
         alertDialog.setTitle("Alert");
         // Setting Dialog Message
         alertDialog.setMessage("Are you sure you want to leave to next screen?");
         // Setting Positive "Yes" Button
         alertDialog.setPositiveButton("YES",
                 new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int which) {
                         //Move to Next screen
                         Intent chnIntent = new Intent(AndroidJSWebView.this, ChennaiIntent.class);  
                         startActivity(chnIntent);  
                     }
                 });
         // Setting Negative "NO" Button
         alertDialog.setNegativeButton("NO",
                 new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int which) {
                         // Cancel Dialog
                         dialog.cancel();
                     }
                 });
         // Showing Alert Message
         alertDialog.show();
    }
}
}