WebView与H5的交互
来源:互联网 发布:蛤是什么意思知乎 编辑:程序博客网 时间:2024/05/22 10:56
H5Activity
package com.asker202.myservice;import android.annotation.SuppressLint;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.webkit.JavascriptInterface;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.widget.Button;import android.widget.Toast;public class H5Activity extends AppCompatActivity implements View.OnClickListener{private WebView wv; private Button btn; private String url="file:///android_asset/js.html";//文件放在assets中 @SuppressLint("JavascriptInterface") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_h5); wv= (WebView) findViewById(R.id.wv); btn= (Button) findViewById(R.id.btn); btn.setOnClickListener(this); WebSettings webSettings = wv.getSettings(); webSettings.setSaveFormData(false); webSettings.setJavaScriptEnabled(true);//设置可响应js webSettings.setSupportZoom(false); wv.setWebChromeClient(new WebChromeClient()); wv.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");//注入本地方法 wv.loadUrl(url); } @Override public void onClick(View v) { if(v.getId()==R.id.btn) { wv.loadUrl("javascript:wave()");//调用js方法 } } final class DemoJavaScriptInterface { DemoJavaScriptInterface() { } /** * @author 孙磊磊 * create at 2016/3/16 16:50 * description 4.2及以上版本,需要加 @JavascriptInterface */ @JavascriptInterface public void click() { runOnUiThread(new Runnable() { @Override public void run() { btn.setText("从网页上调用本地方法"); Toast.makeText(H5Activity.this,"从网页上点击了我",Toast.LENGTH_LONG).show(); } }); } }}js.html
<html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body> <script> function wave() { alert("1"); document.getElementById("text").innerHTML="从Android中调用js"; alert("2"); } function js2android(){ document.getElementById("text").innerHTML="从js中调用Android代码"; demo.click(); } </script> <button type="button" onclick="javascript:demo.click()">点击这里</button> <br/> <button type="button" onclick="windows:demo.click()">点击这里</button> <br/> <button type="button" onclick="demo.click()">点击这里</button> <br/> <button type="button" onclick="js2android()">点击这里</button> <p id="text"></p></body></html>布局文件也加上吧
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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:orientation="vertical" tools:context="com.asker202.myservice.H5Activity"> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="调用js"/> <WebView android:id="@+id/wv" android:layout_width="match_parent" android:layout_height="match_parent"></WebView></LinearLayout>
0 0
- WebView与H5的交互
- Webview 与h5的交互
- Webview 与h5的交互
- webview与h5交互
- iOS webview与H5的交互
- webview和h5的交互
- iOS webView与H5的交互(返回页面的处理)
- iOS webView与H5的交互(返回页面的处理)
- 基于cordova实现的webview实现与h5的交互
- iOS webView与H5的交互(返回页面的处理)
- 关于webview与H5属性设置以及交互的总结
- Android利用WebView与h5交互的几种形式
- android webview与h5交互时所遇到的坑
- android webview设置以及与h5交互
- WebView Native与H5交互—jsbridge
- Android 嵌套H5 webview 与 js 交互
- Android webview与H5 Js 交互
- Android的webView和h5+js交互
- java基础
- ubuntu中apt-get安装与默认路径
- 2014年第五届蓝桥杯B组C/C++省赛题解
- 文件管理系统课堂笔记
- Mysql中文乱码解决方案
- WebView与H5的交互
- Html_tag
- python标准库学习2-OS
- 2015蓝桥杯 生命之树
- 20+条MySQL性能优化的最佳经验
- 在Android中使用Gradle
- Android--计算器小程序
- JavaScript学习笔记:数组求和方法(待续中...)
- Android显示webview加载的网页源码