Android应用中怎么利用WebView与Javascript进行交互

  

Android应用中怎么利用WebView与Javascript进行交互?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

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

(一)Android部分:

布局代码:

& lt; LinearLayout  xmlns: Android=癶ttp://schemas.android.com/apk/res/android"   xmlns:才能工具=癶ttp://schemas.android.com/tools"   android:才能layout_width=癿atch_parent"   android:才能layout_height=癿atch_parent"   android:才能focusable=皌rue"   android:才能focusableInTouchMode=皌rue"   android:才能取向=皏ertical"   android:才能填充=? dp"   工具:才能上下文=?MainActivity"祝辞   ,& lt; LinearLayout   ,,,android: layout_width=癿atch_parent"   ,,,android: layout_height=皐rap_content"比;   ,,EditText & lt;   ,,,,,android: id=癅 + id/input_et"   ,,,,,android: layout_width=? dp"   ,,,,,android: layout_height=皐rap_content"   ,,,,,android:单行模式=皌rue"   ,,,,,android: layout_weight=?”;   ,,,,,android:提示=扒胧淙胄畔ⅰ?/比;   ,,,& lt;按钮   ,,,,,android:文本=癑ava调用JS"   ,,,,,android: layout_width=皐rap_content"   ,,,,,android: layout_height=皐rap_content"   ,,,,,android: onClick=皊endInfoToJs",/比;   & lt;才能/LinearLayout>   ,& lt; WebView   ,,,android: id=癅 + id/webView"   ,,,android: layout_width=癿atch_parent"   ,,,android: layout_height=癿atch_parent",/比;   & lt;/LinearLayout>

活动代码:

/* *   ,* Android  WebView 与,Javascript 交互。   ,*/public  class  MainActivity  extends  ActionBarActivity  {   private 才能;WebView  webView;   @SuppressLint才能({“SetJavaScriptEnabled",,“AddJavascriptInterface"})   @Override才能   protected 才能;void  onCreate (Bundle  savedInstanceState), {   ,,,super.onCreate (savedInstanceState);   ,,,setContentView (R.layout.activity_main);   ,,,webView =, (WebView), findViewById (R.id.webView);   ,,,webView.setVerticalScrollbarOverlay(真正的);   ,,,//设置WebView支持JavaScript   ,,,webView.getSettings () .setJavaScriptEnabled(真正的);   ,,,String  url =,“http://192.168.1.27/js_17_android_webview.html";   ,,,webView.loadUrl (url);   ,,,//在js中调用本地java方法   ,,,webView.addJavascriptInterface (new  JsInterface(这个),“AndroidWebView");   ,,,//添加客户端支持   ,,,webView.setWebChromeClient (new  WebChromeClient ());   ,,}   private 才能class  JsInterface  {   ,,,private  Context  mContext;   ,,,public  JsInterface (Context 上下文),{   ,,,,,this.mContext =,上下文;   ,,,}   ,,,//在js中调用window.AndroidWebView.showInfoFromJs(名字),便会触发此方法。   ,,@JavascriptInterface   ,,,public  void  showInfoFromJs (String 名称),{   ,,,,,Toast.makeText (mContext、,名称,Toast.LENGTH_SHORT),告诉();   ,,,}   ,,}//在java中才能调用js代码   public 才能;void  sendInfoToJs (View 视图),{   ,,,String  msg =, ((EditText), findViewById (R.id.input_et)) .getText () .toString ();   ,,,//调用js中的函数:showInfoFromJava(味精)   ,,,webView.loadUrl (“javascript: showInfoFromJava (& # 39;“, +, msg  +,“& # 39;)“);   ,,}   }

(二)网页代码:

& lt; ! DOCTYPE  html  PUBLIC “-//W3C//DTD  XHTML  1.0,过渡//EN"   “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"比;   & lt; html>   & lt; meta  http-equiv=癈ontent-Type",内容=皌ext/html;, charset=utf-8",/比;   & lt; meta  http-equiv=癈ontent-Language",内容=皕h-cn",/比;   & lt; title> Android  WebView 与,Javascript 交互& lt;/title>   & lt; head>   ,& lt; style>   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android应用中怎么利用WebView与Javascript进行交互