android 向webview传值

android中可以使用WebView加载网页,同时Android端的java代码可以与网页上的javascript代码之间相互调用。

效果图:

(一)Android部分:

布局代码:

<span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><LinearLayout</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">xmlns:android</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"http://schemas.android.com/apk/res/android"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">xmlns:tools</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"http://schemas.android.com/tools"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_width</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_height</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:focusable</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"true"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:focusableInTouchMode</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"true"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:orientation</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"vertical"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:padding</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"8dp"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">tools:context</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">".MainActivity"</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);">></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

    </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><LinearLayout</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_width</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_height</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"wrap_content"</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);">></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

        </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><EditText</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:id</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"@+id/input_et"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_width</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"0dp"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_height</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"wrap_content"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:singleLine</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"true"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_weight</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"1"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:hint</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"请输入信息"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);">/></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

        </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><Button</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:text</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"Java调用JS"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_width</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"wrap_content"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_height</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"wrap_content"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:onClick</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"sendInfoToJs"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);">/></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"></LinearLayout></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

    </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><WebView</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:id</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"@+id/webView"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_width</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_height</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);">/></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"></LinearLayout></span>

Activity代码:

<span class="com" style="box-sizing: border-box; color: rgb(136, 0, 0);">/**
 * Android WebView 与 Javascript 交互。
 */</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
</span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">public</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">class</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">MainActivity</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">extends</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">ActionBarActivity</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">private</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">WebView</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> webView</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

    </span><span class="lit" style="box-sizing: border-box; color: rgb(0, 102, 102);">@SuppressLint</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">({</span><span class="str" style="box-sizing: border-box; color: rgb(0, 136, 0);">"SetJavaScriptEnabled"</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">,</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="str" style="box-sizing: border-box; color: rgb(0, 136, 0);">"AddJavascriptInterface"</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">})</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="lit" style="box-sizing: border-box; color: rgb(0, 102, 102);">@Override</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">protected</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">void</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> onCreate</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">Bundle</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> savedInstanceState</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">)</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">super</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">onCreate</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">savedInstanceState</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">);</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        setContentView</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">R</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">layout</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">activity_main</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">);</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

        webView </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">WebView</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">)</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> findViewById</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">R</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">id</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">webView</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">);</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

        webView</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">setVerticalScrollbarOverlay</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">true</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">);</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="com" style="box-sizing: border-box; color: rgb(136, 0, 0);">//设置WebView支持JavaScript</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        webView</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">getSettings</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">().</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">setJavaScriptEnabled</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">true</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">);</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

        </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">String</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> url </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="str" style="box-sizing: border-box; color: rgb(0, 136, 0);">"http://192.168.1.27/js_17_android_webview.html"</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        webView</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">loadUrl</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">url</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">);</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

        </span><span class="com" style="box-sizing: border-box; color: rgb(136, 0, 0);">//在js中调用本地java方法</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        webView</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">addJavascriptInterface</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">new</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">JsInterface</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">this</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">),</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="str" style="box-sizing: border-box; color: rgb(0, 136, 0);">"AndroidWebView"</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">);</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

        </span><span class="com" style="box-sizing: border-box; color: rgb(136, 0, 0);">//添加客户端支持</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        webView</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">setWebChromeClient</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">new</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">WebChromeClient</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">());</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

    </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">private</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">class</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">JsInterface</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">private</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">Context</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> mContext</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

        </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">public</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">JsInterface</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">Context</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> context</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">)</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">this</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">mContext </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> context</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

        </span><span class="com" style="box-sizing: border-box; color: rgb(136, 0, 0);">//在js中调用window.AndroidWebView.showInfoFromJs(name),便会触发此方法。</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="lit" style="box-sizing: border-box; color: rgb(0, 102, 102);">@JavascriptInterface</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">public</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">void</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> showInfoFromJs</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">String</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> name</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">)</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
            </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">Toast</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">makeText</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">mContext</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">,</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> name</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">,</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">Toast</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">LENGTH_SHORT</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">).</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">show</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">();</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

    </span><span class="com" style="box-sizing: border-box; color: rgb(136, 0, 0);">//在java中调用js代码</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">public</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">void</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> sendInfoToJs</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">View</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> view</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">)</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">String</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> msg </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">((</span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">EditText</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">)</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> findViewById</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">R</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">id</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">input_et</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">)).</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">getText</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">().</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">toString</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">();</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        </span><span class="com" style="box-sizing: border-box; color: rgb(136, 0, 0);">//调用js中的函数:showInfoFromJava(msg)</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
        webView</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">loadUrl</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="str" style="box-sizing: border-box; color: rgb(0, 136, 0);">"javascript:showInfoFromJava('"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">+</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> msg </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">+</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="str" style="box-sizing: border-box; color: rgb(0, 136, 0);">"')"</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">);</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span>

(二)网页代码:

<span class="dec" style="box-sizing: border-box; color: rgb(102, 0, 102);"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><html></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><meta</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">http-equiv</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"Content-Type"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">content</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"text/html; charset=utf-8"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);">/></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><meta</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">http-equiv</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"Content-Language"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">content</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"zh-cn"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);">/></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><title></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">Android WebView 与 Javascript 交互</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"></title></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><head></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><style></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  body </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">background</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">-</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">color</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:#</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">e6e6e6</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

  </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">rect
  </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    color</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">white</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    font</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">-</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">family</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:</span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">Verdana</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">,</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">Arial</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">,</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">Helvetica</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">,</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> sans</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">-</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">serif</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    font</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">-</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">size</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:</span><span class="lit" style="box-sizing: border-box; color: rgb(0, 102, 102);">16px</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    width</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:</span><span class="lit" style="box-sizing: border-box; color: rgb(0, 102, 102);">100px</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    padding</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:</span><span class="lit" style="box-sizing: border-box; color: rgb(0, 102, 102);">6px</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    background</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">-</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">color</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:#</span><span class="lit" style="box-sizing: border-box; color: rgb(0, 102, 102);">98bf21</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    text</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">-</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">decoration</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">none</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    text</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">-</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">align</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">center</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    border</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">none</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    cursor</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">pointer</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

  </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">inputStyle </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">{</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">font</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">-</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">size</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:</span><span class="lit" style="box-sizing: border-box; color: rgb(0, 102, 102);">16px</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">padding</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">:</span><span class="lit" style="box-sizing: border-box; color: rgb(0, 102, 102);">6px</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"></style></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"></head></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><body></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><p></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">测试Android WebView 与 Javascript 交互</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"></p></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><input</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">id</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"name_input"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">class</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"inputStyle"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">type</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"text"</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);">/></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><a</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">class</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"rect"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> </span><span class="atn" style="box-sizing: border-box; color: rgb(102, 0, 102);">onclick</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">sendInfoToJava</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">()</span><span class="atv" style="box-sizing: border-box; color: rgb(0, 136, 0);">"</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);">></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">JS调用Java</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"></a></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

  </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"><script></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">function</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> sendInfoToJava</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(){</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="com" style="box-sizing: border-box; color: rgb(136, 0, 0);">//调用android程序中的方法,并传递参数</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">var</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> name </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">=</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> document</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">getElementById</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="str" style="box-sizing: border-box; color: rgb(0, 136, 0);">"name_input"</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">).</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">value</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">;</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    window</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="typ" style="box-sizing: border-box; color: rgb(102, 0, 102);">AndroidWebView</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">.</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">showInfoFromJs</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">name</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">);</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

  </span><span class="com" style="box-sizing: border-box; color: rgb(136, 0, 0);">//在android代码中调用此方法</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  </span><span class="kwd" style="box-sizing: border-box; color: rgb(0, 0, 136);">function</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);"> showInfoFromJava</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">msg</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">){</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
    alert</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">(</span><span class="str" style="box-sizing: border-box; color: rgb(0, 136, 0);">"来自客户端的信息:"</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">+</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">msg</span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">);</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  </span><span class="pun" style="box-sizing: border-box; color: rgb(102, 102, 0);">}</span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
  </span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"></script></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">

</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"></body></span><span class="pln" style="box-sizing: border-box; color: rgb(0, 0, 0);">
</span><span class="tag" style="box-sizing: border-box; color: rgb(0, 0, 136);"></html></span>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏酷玩时刻

微信、支付宝App支付-JPay0.0.2发布JPay

对微信App支付、支付宝App支付的二次封装,对外提供一个相对简单的接口以及支付结果的回调

13940
来自专栏向治洪

顺序广播和无序广播

普通广播(Normal Broadcast): 一,优缺点:和有序广播的优缺点相反! 二,发送广播的方法:sendBroadcast() 有序广播(Or...

24390
来自专栏Android干货

安卓开发_关于WebView使用链接时调用浏览器显示的问题

371150
来自专栏Vamei实验室

安卓第六夜 凡高的自画像

在上一讲中,我已经制作了一个简单的Android应用。项目的主要文件包括: MainActivity.java activity_main.xml 在这一讲,我...

22070
来自专栏知识分享

android客服端+eps8266+单片机+路由器之远程控制系统

用android客服端+eps8266+单片机+路由器做了一个远程控制的系统,因为自己是在实验室里,所以把实验室的门,灯做成了远程控制的。 控制距离有多远---...

78560
来自专栏Android常用基础

MVC、MVP、MVVM的理解与使用

对于MVC、MVP、MVVM,这都是Android开发的一种架构,随着日积月累的开发,很对人对这几种模式是再熟悉不过了。不过当初我在学习的时候是因为看一个开源项...

10010
来自专栏沃趣科技

Oracle 12c ASM专题|Flex Diskgroup相关概念

原文链接 https://martincarstenbach.wordpress.com/2017/07/11/12-2-new-feature-the-fle...

38470
来自专栏飞雪无情的博客

Android Activity的生命周期

通过上一节“Android系列之四:Android项目的目录结构”我们已经知道了什么是Activity,那么为什么我们创建一个Activity的导出类的时候为什...

9230
来自专栏Android点滴积累

屏幕旋转时调用PopupWindow update方法更新位置失效的问题及解决方案

   接到一个博友的反馈,在屏幕旋转时调用 PopupWindow 的 update 方法失效。使用场景如下:在一个 Activity 中监听屏幕旋转事件,在A...

13600
来自专栏Android小菜鸡

助你快速搭建一个健壮可控的WebApp

14230

扫码关注云+社区

领取腾讯云代金券