calling javascript function from an android activity

后端 未结 7 1766
心在旅途
心在旅途 2021-01-06 01:13

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()\"

相关标签:
7条回答
  • 2021-01-06 01:45

    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.

    0 讨论(0)
  • 2021-01-06 01:45

    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));
    
    0 讨论(0)
  • 2021-01-06 01:50

    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)+")");
    
    }
    
    0 讨论(0)
  • 2021-01-06 01:51

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

    0 讨论(0)
  • 2021-01-06 01:54

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

    webView.setWebViewClient(new WebViewClient() {
    
       public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:change(i)");
        }
    });
    
    0 讨论(0)
  • 2021-01-06 01:54

     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()+"\")"); }
            });
    
    0 讨论(0)
提交回复
热议问题