Android中WebView和JS交互之彈出框問題

        混合開發時,最常見的就是WebView和JS的交互操作,其中有一個操作就是JS的彈出框在Android的WebView中不能彈出問題,相信做過混合開發的童鞋都有遇到這個問題。因爲默認情況下,Android WebView是不支持js的Alert(),Confirm(),Prompt()函數的彈出提示框的.即使設置了setJavaScriptEnabled(true);也是沒用的.那麼,如何才能讓WebView可以支持js的這3個函數呢.可以通過設置WebChromeClient對象來完成.WebChromeClient主要輔助WebView處理Javascript的對話框、網站圖標、網站title、加載進度等等.下面談談解決方案,首先還是放出效果圖:

        這是html中的點擊提交後的代碼截圖,下面是手機端涉及界面的彈出框效果圖:

        好了,具體的解決方法:

        網絡權限必須要有的:

<uses-permission android:name="android.permission.INTERNET" />

        然後是WebView配置允許和JS交互:

webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        再然後就是這裏最重要的方法了:

webView.setWebChromeClient(new WebChromeClient(){
    @Override
    public boolean onJsAlert(WebView view, String url, final String message, final JsResult result) {

        new AlertOpeUtil(LookHelpActivity.this, new IAlertCallBack() {
            @Override
            public void onOkCallBack() {
                if(message.equals("提交成功!")){
                    finish();
                }else {
                    result.confirm();
                }
            }

            @Override
            public void onCancelBack() {

            }
        }).showDialog2("提示:",message);
        return true;
    }
    //設置響應js 的Confirm()函數
    @Override
    public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
        new AlertOpeUtil(LookHelpActivity.this, new IAlertCallBack() {
            @Override
            public void onOkCallBack() {
                result.confirm();
            }

            @Override
            public void onCancelBack() {
                result.cancel();
            }
        }).showDialog("提示:",message);
        return true;
    }
    //設置響應js 的Prompt()函數
    @Override
    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
        final View v = View.inflate(LookHelpActivity.this, R.layout.prompt_dialog, null);
        ((TextView) v.findViewById(R.id.prompt_message_text)).setText(message);
        ((EditText) v.findViewById(R.id.prompt_input_field)).setText(defaultValue);
        AlertDialog.Builder b = new AlertDialog.Builder(LookHelpActivity.this);
        b.setTitle("Prompt");
        b.setView(v);
        b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String value = ((EditText) v.findViewById(R.id.prompt_input_field)).getText().toString();
                //result.confirm(value);
            }
        });
        b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               // result.cancel();
            }
        });
        b.create().show();
        return true;
    }
});

        三個重寫的方法分別對應於JS中的alert(),confirm(),prompt()方法的處理。就是相當於把JS中的彈出框分別轉換成了Android中的彈出框來處理。結束大笑

   


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章