项目中嵌入了H5 页面,这个时候就需要拿到H5 input中的内容进行数据传递,先看实现的效果图
这就是实现的效果,页面效果什么的可以忽略不计了,主要看功能,下面贴代码
首先看MainActivity的布局,很简单,就是一个WebView
<WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/ll_top" />
然后是网页内容,这个网页内容,一般的是你们前端给你们写的 ,只是你们两个人确定好方法就可以,这里我直接放在本地了
<!DOCTYPE > <html xmlns="http://www.w3.org/1999/xhtml" debug="true"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="viewport" content="target-densitydpi=device-dpi"/> <script type="text/javascript"> function init() { var testVal = document.getElementById(\'mytextId\').value; AndroidFunction.showToast(testVal); } </script> </head> <body> <div style="float: left;width: 50%;"> <input type="text" style="width: 180px;" name="myText" id="mytextId"/> </div> <div style="clear: both;height: 3px;"></div> <div> <input value="submit" type="button" name="submit" id="btnSubmit" onclick="javascript:return init();"/> </div> </body> </html>
再就是MainActivity.java的具体实现和方法
public class MainActivity2 extends AppCompatActivity { private WebView webView; private LinearLayout ll_root; private EditText et_user; private ImageView iv_btn_return; final Handler myHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_user = (EditText) findViewById(R.id.et_user); webView = (WebView) findViewById(R.id.webView); initWebView(); } //初始化WebView private void initWebView() { //不跳转到其他浏览器 webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); final JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(this); webView.getSettings().setLightTouchEnabled(true); webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction"); webView.loadUrl("file:///android_asset/JavaAndJavaScriptCall2.html"); } //按钮的点击事件 public void click(View view) { } //在页面销毁的时候将webView移除 @Override protected void onDestroy() { super.onDestroy(); // ll_root.removeView(webView); webView.stopLoading(); webView.removeAllViews(); webView.destroy(); webView = null; } public class JavaScriptInterface { Context mContext; JavaScriptInterface(Context c) { mContext = c; } @JavascriptInterface public void showToast(String webMessage) { final String msgeToast = webMessage; myHandler.post(new Runnable() { @Override public void run() { // This gets executed on the UI thread so it can safely modify Views Log.e("toast", "msgToast:" + msgeToast); Toast.makeText(mContext, "msgToast:" + msgeToast, Toast.LENGTH_SHORT).show(); } }); Toast.makeText(mContext, webMessage, Toast.LENGTH_SHORT).show(); } } }
此博客不太规范,也没有进行排版美化,仅用来记录自己工作中的问题和学习使用