android webview JS傳值 prompt

來源:互聯網
上載者:User

標籤:rom   file   position   pre   round   表輸入   save   inner   uil   

1.

<!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>

2.

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;            }        });    }}

3

<?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>

  

android webview JS傳值 prompt

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.