Android java与HTML的交互
来源:互联网 发布:linux# 编辑:程序博客网 时间:2024/06/06 16:25
分析以上实现的功能要点:
- title和title中的返回按钮是用HTML实现的,点击返回按钮回到上一个activity
- 输入框和HTML按钮都是HTML部分,在输入框中输入文字,然后点击HTML按钮会显示一个Toast,如图(name = 张三丰)
- 底部的按钮ANDROID按钮会在HTML的输入框中显示张三丰
Android xml布局如下:
<?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:id="@+id/activity_web_view" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="luoxiang.com.webviewh5demo.WebViewActivity"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent"> </WebView> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="android按钮" /></RelativeLayout>java代码:
package luoxiang.com.webviewh5demo;import android.content.Context;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.webkit.JavascriptInterface;import android.webkit.WebSettings;import android.webkit.WebView;import android.widget.Button;import android.widget.Toast;public class WebViewActivity extends AppCompatActivity implements View.OnClickListener{ private WebAppInterface mAppInterface; private WebView mWebView; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view); mAppInterface = new WebAppInterface(this); mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(this); initWV(); } private void initWV() { mWebView = (WebView) findViewById(R.id.webView); mWebView.loadUrl("file:///android_asset/index.html");//加载本地的html WebSettings settings = mWebView.getSettings();//获取WebSettings对象,利用WebSettings配置WebView settings.setJavaScriptEnabled(true);//设置允许执行JS脚本 mWebView.addJavascriptInterface(mAppInterface,"app");//添加HTML与AAndroid的通讯接口 } @Override public void onClick(View view) { mAppInterface.showName("张三丰"); } class WebAppInterface{ private Context context; public WebAppInterface(Context context) { this.context = context; } //////////////////////////////// 下面是JS调用安卓的的方法 @JavascriptInterface public void sayHello(String name){ Toast.makeText(context,"name="+name,Toast.LENGTH_SHORT).show(); } @JavascriptInterface public void back(){ finish(); } ////////////////////////////////// 下面是安卓调用js的的方法 @JavascriptInterface public void showName(final String name){ runOnUiThread(new Runnable() { //Android更新UI需要在主线程 @Override public void run() { mWebView.loadUrl("javascript:showName('"+name+"')"); } }); } }}
HTML代码:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title>HTML 测试</title><style type="text/css">*{margin: 0;padding: 0;}.title{position: relative;width: 100%;height: 50px;background-color: #f90;}.back{position: absolute;font-size: 24px;background-color:#f92;width: 60px;height: 50px;}.title h2{position: absolute;right: 30%;height: 50px;float: left;line-height: 50px;}.content{margin: 3px;}#textName{display: block;width: 100%;height: 35px;}.button{display: block;width: 100%;height: 35px;font-size: 24px;border: none;background-color: #f95;}</style><script type="text/javascript">function sayHello() {var name = document.getElementById('textName').value;app.sayHello(name);}function back(){app.back();}function showName(name){document.getElementById('textName').value = name;}</script></head><body><div class="title"><input class="back" type="button" onclick="back()" value="返回"><h2>HTML5 Test</h2></div><div class="content"><input id="textName" type="text"><button class="button" onclick="sayHello()">HTML按钮</button></div></body></html>
HTML代码是使用本地的,放在assets目录中。
- HTML与Android的交互总结为四个步骤:
- 配置可执行JS脚本 websettings.setJavaScriptEnabled(true);
- 添加通讯接口 webView.addJavaScriptInterface(Interface,"InterfaceName");
- js调用Android InterfaceName.MethodName();
- Android调用js webView.loadUrl("javaScript:functionName");
0 0
- Android java与HTML的交互
- android与html交互
- android与html交互
- Android 和 Html 之间的交互(Android(Java)与JavaScript(HTML))
- html与iOS/Android的兼容交互
- Android 与 HTML + Js的交互
- Android与HTML+JS的交互
- Android与HTML的JS 交互
- android js与html 交互
- Android与Html&JS交互
- Android与HTML+JS交互
- html与android交互总结
- Android与HTML+JS交互
- android与js+HTML交互
- Android HTML与原生交互
- android 中本地java代码与html交互总结
- Android中WebView载入本地HTML代码并实现Java与JavaScript交互的例子
- Android与html交互所遇到的问题记录
- 求a和b的最大公约数 gcd(a ,b)= gcd(b, a%b) 例:gcd(36,24) =gcd(24, 12 ) =gcd(12, 0 )
- 【LeetCode】 394. Decode String
- QT内存管理机制
- 算法竞赛入门经典第一章
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) B. Bash's Big Day 数论+贪心
- Android java与HTML的交互
- ubuntu下编辑ros(c++)项目
- JAVA 类的加载顺序
- iOS开发-通知传值(NSNotifacationCenter)处理界面操作及其细节
- The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path
- 集合框架_统计字符串中每个字符出现的次数案例代码实现
- 求π
- 数据库事务
- 51nod 1575 Gcd and Lcm