从 android 活动调用 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15673509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:37:53  来源:igfitidea点击:

calling javascript function from an android activity

javascriptandroidhtml

提问by user2206969

I wan to call a javascript function from an android activity but it doesn't seem to work. I have used the android webview function webview.loadUrl("javascript:function()"); This is my android code:

我想从 android 活动调用 javascript 函数,但它似乎不起作用。我已经使用了 android webview 函数 webview.loadUrl("javascript:function()"); 这是我的安卓代码:

package com.example.web;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView webview;
    int i=30;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webview = (WebView)findViewById(R.id.webView1);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("file:///android_asset/index.html");
        webview.loadUrl("javascript:change(i)");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

This is my html code:

这是我的 html 代码:

 <html>
<body>
<div id="texta">text</div>
<script>

function change(num){
document.getElementById("texta").innerHTML=num;
}

</script>

</body>
</html>

回答by Tushar

It's likely your page isn't fully loaded by the time you're trying to execute the javascript. Can you try:

当您尝试执行 javascript 时,您的页面可能尚未完全加载。你能试一下吗:

webView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:change(i)");
    }
});

回答by Han Tran

Try:

尝试:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webview = (WebView)findViewById(R.id.webView1);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("file:///android_asset/index.html");
    webview.loadUrl("javascript:change("+String.valueOf(i)+")");

}

回答by hRb

Try it:

试试看:

String URL = "file:///android_asset/filename.html";
webviewBrowser=(WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(webview.loadUrl(URL));

回答by Christoforos

Try to change this :

尝试改变这一点:

webview.loadUrl("javascript:change(i)");

to this

对此

webview.loadUrl("javascript:change(" + i +")");

the way you have write it the "i" is not the variable "i" from your java code but just a String named "i".

您编写它的方式“i”不是您的java代码中的变量“i”,而只是一个名为“i”的字符串。

回答by Naveed Ahmad

What is "i", If "i" is a number then Specify the Number, if "i" is a character then kindly enclose it in double Quotes, you are calling a method of some arguments, so kindly correct your mistake.

什么是“i”,如果“i”是数字,则指定数字,如果“i”是字符,请将其括在双引号中,您正在调用某些参数的方法,因此请纠正您的错误。

回答by Sanjay Kumar

try webview.loadUrl("javascript:change(\""+i"\")");

试试 webview.loadUrl("javascript:change(\""+i"\")");

回答by Dhina k

enter image description here

在此处输入图片说明

 protected void onCreate(Bundle savedInstanceState) 
 {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   final Activity MyActivity = this;
   text=(EditText)findViewById(R.id.textValue);
   Show=(Button)findViewById(R.id.textButton);
   webView= (WebView) findViewById(R.id.webView);
   getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
                                    Window.PROGRESS_VISIBILITY_ON);
   webView.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {

   MyActivity .setTitle("Loading...");
     MyActivity .setProgress(progress * 100);

     if (progress == 100)
     MyActivity .setTitle("Android Dhina");
    }
   });
     webView.setWebViewClient(new WebViewClient());
     webView.addJavascriptInterface(new WebAppInterface(this), "Android");

     webView.getSettings().setJavaScriptEnabled(true);
     webView.loadUrl("file:///android_asset/web.html");

     Show.setOnClickListener(new OnClickListener()
      {
       @Override
       public void onClick(View v) 
       {
         webView.loadUrl("javascript:callFromAndroidActivity
                          (\""+text.getText().toString()+"\")"); }
        });