混合开发-webview和原生交互
来源:互联网 发布:电信宽带免费提速软件 编辑:程序博客网 时间:2024/06/08 09:14
混合开发在移动开发中很常见,比如qq中的运动,厘米秀等功能都是用网页实现的。
混合开发中一个重要的功能就是网页和原生接口的数据交互,下面将实现一个小demo.
新建一个activity,布局如下
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.h.learn.WebViewActivity"> <WebView android:layout_weight="1" android:layout_width="match_parent" android:layout_height="fill_parent" android:id="@+id/my_web_view" /> <LinearLayout android:background="@drawable/webview_bg" android:layout_weight="1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/edit_info" android:text="text" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_confirm" android:text="确定" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/log_text_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout></LinearLayout>
布局中定义了一个webview,在屏幕上半部分。
底部是原生的一个输入框和按钮,输入框用于输入数据,按钮用于调用网页中的接口。
网页中也应该是类似的,一个输入框和一个调用原生接口的按钮。
assets文件夹中的网页代码如下:
<!DOCTYPE html><html><head> <title></title></head><body> <script> //供原生代码调用 function showEditInfo(str){ alert(str); }; //调用原生接口,并把网页中文本框的内容传过去 function callNative() { var str=document.getElementById("mytext").value; window.webview.actionFromJsWithParam(str); }; //alert(1); //showEditInfo('123'); </script> <input type="text" value="123" name="mytext" id="mytext"> <button onClick="callNative()">调用原生接口</button></body></html>
public class WebViewActivity extends AppCompatActivity { @BindView(R.id.my_web_view)WebView webview; @BindView(R.id.edit_info)EditText editInfo; @BindView(R.id.log_text_view)TextView logTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view); ButterKnife.bind(this); //webview = (WebView) findViewById(R.id.my_web_view); Log.d("flag--","onCreate(WebViewActivity.java:20)-->>"+webview); initView(); } //调用js接口,并把editText中的内容传到网页接口中 @OnClick(R.id.btn_confirm) public void confirm(){ Log.d("flag--","confirm(WebViewActivity.java:35)-->>"+"javascript:showEditInfo('"+editInfo.getText()+"')"); webview.loadUrl("javascript:showEditInfo('"+editInfo.getText()+"')"); } private void initView() { //加载网页 webview.loadUrl("file:///android_asset/webview.html"); //允许弹框 webview.setWebChromeClient(new WebChromeClient()); WebSettings webSettings= webview.getSettings(); //允许运行js webSettings.setJavaScriptEnabled(true); webview.addJavascriptInterface(this, "webview"); } //供js调用的接口 @android.webkit.JavascriptInterface public void actionFromJsWithParam(final String str) { runOnUiThread(new Runnable() { @Override public void run() { String text = logTextView.getText() + "\njs调用了Native函数传递参数:" + str; logTextView.setText(text); } }); }}
webview.addJavascriptInterface(this, “webview”);中第二个参数可以自定义,但要和js中相对应,因此js中调用原生接口的方法为 window.webview.actionFromJsWithParam(str);
最终效果:
常用框架:
https://weex.apache.org/cn/guide/
http://www.ionic.wang/js_doc-index.html
http://www.wex5.com/wex5/
阅读全文
0 0
- 混合开发-webview和原生交互
- webview和原生应用交互
- Android中的webview和原生交互
- 安卓混合开发——Android原生和H5数据交互,详细Demo
- 安卓混合开发——原生Java和H5交互,保证你一看就懂!
- 混合开发之webView加载html,android 和 html之间进行数据交互
- WebRTC原生开发和混合开发优缺点
- Android混合开发之WebView与Javascript交互
- Android混合开发之WebView与Javascript交互
- Android混合开发之WebView与Javascript交互
- Android混合开发之WebView与Js交互
- Android混合开发之WebView与Javascript交互
- Android混合开发之WebView与Javascript交互
- iOS原生WebView中JavaScript和OC交互
- Hybrid APP 混合开发模式的选择之路(五:原生和H5的交互原理)
- Hybrid APP 混合开发模式的选择之路(五:原生和H5的交互原理)
- Android混合开发 java和js交互
- 混合开发,原生+html5
- iOS textview加载html并改变图片大小
- 1971-判断素数
- Mastering Web Application Development with AngularJS-Packt 2013 读书笔记(不错的好书!)
- Node -- 构建HTTP服务
- HadoopHelper使用说明
- 混合开发-webview和原生交互
- 利用DBLink+JOB实现两个Oracle数据库之间的数据同步
- 关于PHP trait、生成器
- 图解HTTP
- 1639
- selenium 重要方法
- java 抽象类 http://www.nightsong.cc/index.php/post/198.html
- Go并发模型之生动我见(一)
- 【Git深入理解】实战经验总结