android webview JS传值 prompt

<!DOCTYPE html>
<html style="min-height: 100%">
 
<head>
    <meta charset="utf-8">
    <title>Carson_Ho</title>
 
    <style>
    * {
        margin: 0px;
        border: 1px solid red;
    }
 
    html,body{
        width: 90%;
        height: 90%;
    }
 
    #divTop {
        position: relative;
        height: 50%;
        background: #ff8888;
        bottom: 0;
    }
    #divBottom{
        display: flex;
        flex-direction: column;
        position: relative;
        height: 50%;
        background: #997777;
    }
    .btn3 {
        flex: 1;
        bottom: 0;
    }
    .in {
        bottom: 0;
    }
 
    </style>
    // JS代码
    <script>
        // Android需要调用的方法
        function callJS(){
            document.getElementById("test").innerHTML = "android button 点击左边 实现'webView.loadUrl('javascript:callJS()');'就可以响应";
            alert("Android调用了JS的callJS方法 实现‘webView.setWebChromeClient’才有响应");
            return "JS函数callJS的返回值";
        }
 
        function callAndroid(){
            /*约定的url协议为:js://webview?arg1=111&arg2=222*/
            document.location = "js://webview?arg1=参数一&arg2=参数2";
         }
 
         function clickprompt(){
            // 调用prompt()
            var result = prompt("js://demo?arg1=调用clickprompt&arg2=222");
            alert("prompt弹出输入的值:" + result);
        }
    </script>
</head>
<body>
<div id="divTop">
    <div id="test"> 0000000000 </div>
    <button onclick="callAndroid()">点击使用callAndroid函数</button>
    <button onclick="clickprompt()">点击调用clickprompt函数 </button>
</div>
<div id="divBottom">
    <button class="btn3">按钮</button>
    <input class="in" value="test"/>
</div>
</body>
</html>
public class MainActivity extends Activity {
    WebView webView;
    Button buttonLeft, buttonRight;
    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        webView = findViewById(R.id.webview);
        buttonLeft = findViewById(R.id.btnLeft);
        buttonRight = findViewById(R.id.btnRight);
        editText = findViewById(R.id.edittext);
 
        WebSettings webSettings = webView.getSettings();
        //允许使用JS
        webSettings.setJavaScriptEnabled(true);
        // 设置允许JS弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
 
        webView.loadUrl("file:///android_asset/index.html");
 
        buttonLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                webView.post(new Runnable() {
                    @Override
                    public void run() {
                        webView.evaluateJavascript("javascript:callJS()", new ValueCallback<String>() {
                            @Override
                            public void onReceiveValue(String s) {
                                //将button显示的文字改成JS返回的字符串
                                buttonLeft.setText(s);
                            }
                        });
                    }
                });
            }
        });
 
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //1. 根据协议的参数,判断是否是所需要的url
                // 一般根据scheme(协议格式) & authority(协议名)判断(前两个参数)
                //假定传入进来的 url = "js://webview?arg1=111&arg2=222"(同时也是约定好的需要拦截的)
                Uri uri = Uri.parse(url);
 
                // 如果url的协议 = 预先约定的 js 协议
                // 就解析往下解析参数
                if (uri.getScheme().equals("js"))
                {
                    // 如果 authority  = 预先约定协议里的 webview,即代表都符合约定的协议
                    // 所以拦截url,下面JS开始调用Android需要的方法
                    if(uri.getAuthority().equals("webview"))
                    {
                        // 执行JS所需要调用的逻辑
                        editText.setText(uri.getQueryParameter("arg1"));
                    }
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
                // 拦截输入框
                // 参数message:代表promt()的内容(不是url)
                // 参数result:代表输入框的返回值
 
                // 根据协议的参数,判断是否是所需要的url
                // 一般根据scheme(协议格式) & authority(协议名)判断(前两个参数)
                //假定传入进来的 url = "js://webview?arg1=111&arg2=222"(同时也是约定好的需要拦截的)
                Uri uri = Uri.parse(url);
                if(uri.getScheme().equals("js"))
                {
                    if(uri.getAuthority().equals("demo"));
                    {
                        result.confirm("js调用了Android的方法成功啦");
                        return true;
                    }
                }
                return super.onJsPrompt(view, url, message, defaultValue, result);
            }
 
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
                b.setTitle("alert1");
                b.setMessage(message);
                b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        result.confirm();
                    }
                });
                b.setCancelable(false);
                b.create().show();
                return true;
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="136dp"
        tools:layout_editor_absoluteY="0dp"
        android:orientation="vertical"
        tools:ignore="MissingConstraints">
 
        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
        </WebView>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_weight="3">
            <Button
                android:id="@+id/btnLeft"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="左边" />
 
            <EditText
                android:id="@+id/edittext"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="0"/>
            <Button
                android:id="@+id/btnRight"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="右边" />
        </LinearLayout>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值