Android高手進階教程(二十)之---Android與JavaScript方法相互調用!

來源:互聯網
上載者:User

標籤:script   rap   UNC   pac   code   line   教程   parent   log   

在Android中通過WebView控制項,可以實現要載入的頁面與Android方法相互調用,我們要實現WebView中的addJavascriptInterface方法,這樣html才能調用android方法,在這裡我個人覺得有點和DWR相似。

為了讓大家容易理解,我寫了一個簡單的Demo,具體步驟如下:

第一步:建立一個Android工程,命名為WebViewDemo(這裡我在assets裡定義了一個html頁面)。

第二步:修改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()");              }          });      }  }

第五步:運行上述工程,查看效果。

                        首介面                                           點擊按鈕時,html內容改變

   點擊html的startGoogleMap啟動地圖應用

Android高手進階教程(二十)之---Android與JavaScript方法相互調用!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.