<html lang='zh-CN' xml:lang='zh-CN' xmlns='http://www.w3.org/1999/xhtml'> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <link rel="shortcut icon" type="image/x-icon" href="/img/favicon.ico?t=1451964198000" /> <title>android studio 如何读取html文件的?如何存放原生资源? - 开源中国社区</title> <meta name="Description" itemprop="description" content="android studio 如何读取html文件的?如何存放原生资源?- 开源中国社区"/> </head> <body> <p>WebView与Javascript交互</p> <div> <button onClick="window.test.actionFromJs()">点击调用Native代码</button> </div> <br/> <div> <button onClick="window.test.actionFromJsWithParam('come from Js')">点击调用Native代码并传递参数</button> </div> <br/> <div id="log_msg">调用打印信息</div> </body></html>
/* **测试webview和 js的互调 */ public class TestActivity extends BaseActivity { private WebView wv_test; private Button btn_test; private TextView logTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test2); wv_test = (WebView) findViewById(R.id.wv_test); btn_test = (Button)findViewById(R.id.btn_test); logTextView = (TextView) findViewById(R.id.logTextView); getData(); } private void getData(){ // 设置支持javascript wv_test.getSettings().setJavaScriptEnabled(true); //启动缓存 wv_test.getSettings().setAppCacheEnabled(true); //设置缓存模式 wv_test.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); Log.e("error"," wv_test.loadUrl(file:///android_asset/index.html)"); //加载网页 wv_test.loadUrl("file:///android_asset/index.html"); wv_test.addJavascriptInterface(this, "test"); btn_test.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // 无参数调用 wv_test.loadUrl("javascript:actionFromNative()"); // 传递参数调用 wv_test.loadUrl("javascript:actionFromNativeWithParam(" + "'come from Native'" + ")"); } }); } @android.webkit.JavascriptInterface public void actionFromJs() { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(TestActivity.this, "js调用了Native函数", Toast.LENGTH_SHORT).show(); String text = logTextView.getText() + "\njs调用了Native函数"; logTextView.setText(text); } }); } @android.webkit.JavascriptInterface public void actionFromJsWithParam(final String str) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(TestActivity.this, "js调用了Native函数传递参数:" + str, Toast.LENGTH_SHORT).show(); String text = logTextView.getText() + "\njs调用了Native函数传递参数:" + str; logTextView.setText(text); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn_test" android:text="访问" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="log" android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:textSize="24sp" android:id="@+id/logTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout> <WebView android:id="@+id/wv_test" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>源码链接 https://github.com/letuszou/WebviewJs
android js webview 互调
最新推荐文章于 2024-04-19 03:49:37 发布