<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                在Android中通過WebView控件,可以實現要加載的頁面與Android方法相互調用,我們要實現WebView中的addJavascriptInterface方法,這樣html才能調用android方法,在這里我個人覺得有點和DWR相似。 為了讓大家容易理解,我寫了一個簡單的Demo,具體步驟如下: 第一步:新建一個Android工程,命名為WebViewDemo(這里我在assets里定義了一個html頁面)。 ![](https://box.kancloud.cn/2016-08-10_57aaf0ea19fa6.gif) 第二步:修改main.xml布局文件,增加了一個WebView控件還有Button控件,代碼如下: ~~~ <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Welcome to Mr Wei's Blog." /> <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Change the webview content" /></LinearLayout> ~~~ 第三步:在assets目錄下新建一個demo.html文件,代碼如下(這里不知道為何多了mce:這幾個東東,<script></script>這樣是對的): ~~~ <html> <mce:script language="javascript"><!-- function fillContent(){ document.getElementById("content").innerHTML = "This Content is showed by Android invoke Javascript function."; } // --></mce:script> <body> <p><a onClick="window.demo.startMap()" href="">Start GoogleMap</a></p> <p id="content"></p> <p>A Demo ----Android and Javascript invoke each other.</p> <p>Author:Frankiewei</p> </body></html> ~~~ ? 第四步:修改主核心程序WebViewDemo.java,代碼如下: ~~~ package com.tutor.webwiewdemo;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.webkit.WebSettings;import android.webkit.WebView;import android.widget.Button;public class WebViewDemo extends Activity { private WebView mWebView; private Button mButton; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } //初始化 private void setupViews() { mWebView = (WebView) findViewById(R.id.webview); WebSettings mWebSettings = mWebView.getSettings(); //加上這句話才能使用javascript方法 mWebSettings.setJavaScriptEnabled(true); //增加接口方法,讓html頁面調用 mWebView.addJavascriptInterface(new Object() { //這里我定義了一個打開地圖應用的方法 public void startMap() { Intent mIntent = new Intent(); ComponentName component = new ComponentName( "com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); mIntent.setComponent(component); startActivity(mIntent); } }, "demo"); //加載頁面 mWebView.loadUrl("file:///android_asset/demo.html"); mButton = (Button) findViewById(R.id.button); //給button添加事件響應,執行JavaScript的fillContent()方法 mButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { mWebView.loadUrl("javascript:fillContent()"); } }); }} ~~~ 第五步:運行上述工程,查看效果。 ? ![](https://box.kancloud.cn/2016-08-10_57aaf0ea2e1c9.gif) ![](https://box.kancloud.cn/2016-08-10_57aaf0ea4f8d5.gif) 首界面 點擊按鈕時,html內容改變 ![](https://box.kancloud.cn/2016-08-10_57aaf0ea69a6e.gif) 點擊html的startGoogleMap啟動地圖應用
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看