在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<abbr id="27omo"></abbr>

<menu id="27omo"><dl id="27omo"></dl></menu>
    • <label id="27omo"><tt id="27omo"></tt></label>

      新聞中心

      EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > Android與JavaScript方法相互調(diào)用

      Android與JavaScript方法相互調(diào)用

      作者: 時間:2016-09-12 來源:網(wǎng)絡(luò) 收藏

      在Android中通過WebView控件,可以實現(xiàn)要加載的頁面與Android方法相互調(diào)用,我們要實現(xiàn)WebView中的addJavascriptInterface方法,這樣html才能調(diào)用android方法。

      本文引用地址:http://www.biyoush.com/article/201609/305031.htm

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

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

      Android與JavaScript方法相互調(diào)用

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

      view plaincopy to clipboardprint?

      android:orientation=vertical

      android:layout_width=fill_parent

      android:layout_height=fill_parent

      >

      android:layout_width=fill_parent

      android:layout_height=wrap_content

      android:text=Welcome to Mr Wei's Blog.

      />

      android:id=@+id/webview

      android:layout_width=fill_parent

      android:layout_height=wrap_content

      />

      android:id=@+id/button

      android:layout_width=fill_parent

      android:layout_height=wrap_content

      android:text=Change the webview content

      />

      第三步:在assets目錄下新建一個demo.html文件,代碼如下(這里不知道為何多了mce:這幾個東東,這樣是對的):

      view plaincopy to clipboardprint?

      Start GoogleMap

      A Demo ----Android and Javascript invoke each other.

      Author:Frankiewei

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

      view plaincopy to clipboardprint?

      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頁面調(diào)用

      mWebView.addJavascriptInterface(new Object() {

      //這里我定義了一個打開地圖應(yīng)用的方法

      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添加事件響應(yīng),執(zhí)行JavaScript的fillContent()方法

      mButton.setOnClickListener(new Button.OnClickListener() {

      public void onClick(View v) {

      mWebView.loadUrl(javascript:fillContent());

      }

      });

      }

      }

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

      Android與JavaScript方法相互調(diào)用

      首界面

      Android與JavaScript方法相互調(diào)用

      點擊按鈕時,html內(nèi)容改變

      Android與JavaScript方法相互調(diào)用

      點擊html的startGoogleMap啟動地圖應(yīng)用



      關(guān)鍵詞:

      評論


      相關(guān)推薦

      技術(shù)專區(qū)

      關(guān)閉
      ×

      “芯”朋友见面大会
      珠海|11.14|泰克“芯”朋友见面大会珠海站|泰克带您从测试角度看半导体的整条产业链,快来报名抢位吧>>