Android Calling JavaScript functions in WebView

前端 未结 7 603
夕颜
夕颜 2020-11-22 04:14

I am trying to call some javascript functions sitting in an html page running inside an android webview. Pretty simple what the code

7条回答
  •  名媛妹妹
    2020-11-22 05:03

    activity_main.xml

    
    
    
        
    
    
    

    MainActivity.java

    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    import com.bluapp.androidview.R;
    
    public class WebViewActivity3 extends AppCompatActivity {
        private WebView webView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_web_view3);
            webView = (WebView) findViewById(R.id.webView);
            webView.setWebViewClient(new WebViewClient());
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("file:///android_asset/webview1.html");
    
    
            webView.setWebViewClient(new WebViewClient(){
                public void onPageFinished(WebView view, String weburl){
                    webView.loadUrl("javascript:testEcho('Javascript function in webview')");
                }
            });
        }
    }
    

    assets file

    
    
    WebView1
    
    
    
    
    
    
    
    
    

提交回复
热议问题