Js_WebView交互
来源:互联网 发布:致远软件招聘 编辑:程序博客网 时间:2024/05/23 01:51
package com.messcat.android_js;import android.content.Context;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.view.View;import android.webkit.JavascriptInterface;import android.webkit.WebChromeClient;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText etJsAlertText; private Button btJsAlertToShow; private WebView webview; private void assignViews() { etJsAlertText = (EditText) findViewById(R.id.et_JsAlert_Text); btJsAlertToShow = (Button) findViewById(R.id.bt_JsAlert_toShow); webview = (WebView) findViewById(R.id.webview); btJsAlertToShow.setOnClickListener(this); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); assignViews(); initWebViewConfig(); } private void initWebViewConfig() { webview.setVerticalScrollbarOverlay(true); webview.getSettings().setJavaScriptEnabled(true); webview.setWebChromeClient(new WebChromeClient()); webview.setWebViewClient(new WebViewClient()); webview.loadUrl("file:///android_asset/AndroidJs.html"); //在js中调用本地java方法 webview.addJavascriptInterface(new JsInterface(this), "AndroidWebView"); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.bt_JsAlert_toShow: sendInfoToJs(); break; default: break; } } /** * js调用Android的接口 */ private class JsInterface { private Context mcontext; public JsInterface(Context mcontext) { this.mcontext = mcontext; } @JavascriptInterface public void showInfoFromJs(String info) { Toast.makeText(mcontext, info, Toast.LENGTH_SHORT).show(); } } /** * 从Android传递参数过去webview */ public void sendInfoToJs() { String info = etJsAlertText.getText().toString().trim().replace(" ", ""); if (TextUtils.isEmpty(info)) { Toast.makeText(MainActivity.this, "请输入参数", Toast.LENGTH_SHORT).show(); return; } //调用js中的函数:showInfoFromJava(msg) webview.loadUrl("javascript:showInfoFromJava('" + info + "')"); }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.messcat.android_js.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <EditText android:id="@+id/et_JsAlert_Text" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="4" /> <Button android:id="@+id/bt_JsAlert_toShow" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:text="JsShowAlert" android:textSize="12sp" /> </LinearLayout> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="60dp" /></RelativeLayout>
<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title></head><body><p>测试Android WebView 与 Javascript 交互</p><input id="name_input" class="inputStyle" type="text" /><a class="rect" onclick="sendInfoToJava()">JS调用Java</a></body><script type="text/javascript">function sendInfoToJava() {//调用android程序中的方法,并传递参数var name = document.getElementById("name_input").value;window.AndroidWebView.showInfoFromJs(name);}//在android代码中调用此方法function showInfoFromJava(msg) {alert("来自客户端的信息:" + msg);}</script></html>
0 0
- Js_WebView交互
- 交互
- 交互
- 同步交互异步交互
- javascript 交互
- 交互脚本
- Hessian交互
- 网页交互
- Android_Http交互
- 声音交互
- cgi交互
- vtk交互
- 线程交互
- 线程交互
- 交互工程师
- 交互设计师
- phonegap_java_js_交互
- 线程交互
- Git学习笔记04—取得项目的Git仓库
- Poj 3311 Hie with the Pie【Floyd+状压dp】
- findviewbyid跟setOnClickListtener的简化
- @ResponseBody不加 会返回404错误
- android WebView解析
- Js_WebView交互
- ORACLE日期时间函数大全
- iOS雷达图 iOS RadarChart实现
- Java基于Socket文件传输示例
- quartz 定时任务的删除 恢复 已经暂停
- Spring MVC @Transactional注解方式事务失效的解决办法
- C++ 网络编程 阻塞I/O模型并发回显服务器
- mysql里的批处理(sql语句)、触发器、事务管理、存储过程
- Java工具类——实现静态图片剪切缩放功能