android与JS交互,互相调用方法,跳转到网页

时间:2025-02-13 07:10:11

main下面New - Folder - Assets Folder,在Assets下面新建一个js_android.html

<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title></title>
    <script type="text/javascript">

              //这个方式是被java调用的
             function androidCallJs(){
             alert("java调用js弹窗");
             }
            </script>
</head>
<body>
<input type="button" οnclick="('js调用Android,我是徐庆臣')" value="js调用java代码"/>
</body>
</html>

activity_main.xml的里面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    xmlns:tools="/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >

    <Button
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="JAVA调用JS方法" />
    <WebView xmlns:andro
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android: >

</WebView>

    <Button
        android:
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="加载百度页面"
        />-->
</LinearLayout>
MainActivity里面,设置webview的显示,映射等

public class MainActivity extends AppCompatActivity {

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private WebView webView;
    private WebView webView1;

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


        btn1 = (Button) findViewById(.btn1);
      //  btn2 = (Button) findViewById(.btn2);
        btn3 = (Button) findViewById(.btn3);

        webView1 = new WebView(this);
        //实例化webview组件
        webView =(WebView) findViewById();
         //(new WebViewClient());

        //加载assets目录下的html文件
        ("file:///android_asset/js_android.html");
        WebSettings webSettings = ();
        (true);
        (true);
        //映射.可以调用js里面的方法
        (new JSInterface(), "jsi");

        (new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return (view, url, message, result);
            }
        });

        //java调用js方法 的点击事件, ("javascript:androidCallJs()");
        (new () {
            @Override
            public void onClick(View view) {
                ("javascript:androidCallJs()");
            }
        });


        //加载百度页面的点击事件
      (new () {
            @Override
            public void onClick(View view) {
                ("");
                setContentView(webView1);
              //  (,"ee",Toast.LENGTH_SHORT).show();
            }
        });
    }



    //设置回退
    //覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
  /*  public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && ()) {
            (); //调用goBack()返回WebView的上一页面
            return true;
        }
        return false;
    }*/


    private final class JSInterface{
        /**
         * 注意这里的@JavascriptInterface注解, target是4.2以上都需要添加这个注解,否则无法调用
         * @param text
         */
        @JavascriptInterface
        public void showToast(String text){
            (getApplicationContext(), text, Toast.LENGTH_SHORT).show();
        }
        @JavascriptInterface
        public void showJsText(String text){
            ("javascript:jsText('"+text+"')");
        }
    }
}