效果如:

Android 使用js调用Java

主要用到一个接口类:MyObject

package com.example.jsdemo;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.webkit.JavascriptInterface;
import android.widget.Toast;


public class MyObject {
    private Context context;
    public MyObject(Context context) {
        this.context = context;
    }

    //将显示Toast和对话框的方法暴露给JS脚本调用
    @JavascriptInterface
    public void showToast(String name) {
        Toast.makeText(context, name+"我是后台追加的", Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void showDialog() {
        new AlertDialog.Builder(context)
                .setTitle("标题")
                .setMessage("我是Java不带参").create().show();
    }
}
View Code

相关文章:

  • 2021-10-21
  • 2022-12-23
  • 2021-04-28
  • 2021-09-20
  • 2022-12-23
  • 2021-07-14
  • 2021-10-26
  • 2021-07-08
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
相关资源
相似解决方案