Android與JavaScript方法相互調用!

來源:互聯網
上載者:User

來自:http://blog.csdn.net/android_tutor/article/details/5853143

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

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

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

第二步:修改main.xml布局檔案,增加了一個WebView控制項還有Button控制項,代碼如下:

[java]
view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TextView    
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:text="Welcome to Mr Wei's Blog."  
  11.         />  
  12.     <WebView  
  13.         android:id="@+id/webview"  
  14.         android:layout_width="fill_parent"   
  15.         android:layout_height="wrap_content"   
  16.     />  
  17.     <Button  
  18.         android:id="@+id/button"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="Change the webview content"  
  22.     />  
  23. </LinearLayout>  

第三步:在assets目錄下建立一個demo.html檔案,代碼如下(這裡不知道為何多了mce:這幾個東東,<script></script>這樣是對的):

[css]
view plaincopy
  1. <html>  
  2.     <mce:script language="javascript"><!--  
  3.    
  4.         function fillContent(){  
  5.             document.getElementById("content").innerHTML =   
  6.                  "This Content is showed by Android invoke Javascript function.";  
  7.         }  
  8.       
  9. // --></mce:script>    
  10.   <body>  
  11.     <p><a onClick="window.demo.startMap()" href="">Start GoogleMap</a></p>  
  12.     <p id="content"></p>  
  13.     <p>A Demo ----Android and Javascript invoke each other.</p>  
  14.     <p>Author:Frankiewei</p>  
  15.   </body>  
  16. </html>   

 

第四步:修改主核心程式WebViewDemo.java,代碼如下:

[java]
view plaincopy
  1. package com.tutor.webwiewdemo;  
  2. import android.app.Activity;  
  3. import android.content.ComponentName;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.webkit.WebSettings;  
  8. import android.webkit.WebView;  
  9. import android.widget.Button;  
  10. public class WebViewDemo extends Activity {  
  11.     private WebView mWebView;  
  12.     private Button mButton;  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         setupViews();  
  17.     }  
  18.     //初始化  
  19.     private void setupViews() {  
  20.         mWebView = (WebView) findViewById(R.id.webview);  
  21.         WebSettings mWebSettings = mWebView.getSettings();  
  22.         //加上這句話才能使用javascript方法  
  23.         mWebSettings.setJavaScriptEnabled(true);  
  24.         //增加介面方法,讓html頁面調用  
  25.         mWebView.addJavascriptInterface(new Object() {  
  26.             //這裡我定義了一個開啟地圖應用的方法  
  27.             public void startMap() {  
  28.                 Intent mIntent = new Intent();  
  29.                 ComponentName component = new ComponentName(  
  30.                         "com.google.android.apps.maps",  
  31.                         "com.google.android.maps.MapsActivity");  
  32.                 mIntent.setComponent(component);  
  33.                 startActivity(mIntent);  
  34.             }  
  35.         }, "demo");  
  36.         //載入頁面  
  37.         mWebView.loadUrl("file:///android_asset/demo.html");  
  38.         mButton = (Button) findViewById(R.id.button);  
  39.         //給button添加事件響應,執行JavaScript的fillContent()方法  
  40.         mButton.setOnClickListener(new Button.OnClickListener() {  
  41.             public void onClick(View v) {  
  42.                 mWebView.loadUrl("javascript:fillContent()");  
  43.             }  
  44.         });  
  45.     }  
  46. }  

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

 

  

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

   點擊html的startGoogleMap啟動地圖應用

相關文章

聯繫我們

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