我使用webView1 +本地html文件,就像一个带有链接的自定义导航栏。
单击链接时,必须在webView2中加载要求的html文件。
这可能吗?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Fixed Portrait orientation
requestWindowFeature(Window.FEATURE_NO_TITLE); // No app title bar
setContentView(R.layout.activity_main);
myNavbarView = (WebView)findViewById(R.id.navbarView);
myNavbarView.getSettings().setJavaScriptEnabled(true);
myNavbarView.setVerticalScrollBarEnabled(false);
myNavbarView.setHorizontalScrollBarEnabled(false);
myNavbarView.setWebViewClient(new WebViewClient());
myNavbarView.loadUrl("file:///android_asset/HTML/navbar.html");
myWebView = (WebView)findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
//hack to load twitter
myWebView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
myWebView.setVerticalScrollBarEnabled(false);
myWebView.loadUrl("file:///android_asset/HTML/index.html");
答案 0 :(得分:1)
不确定为什么要使用这样的两个webView。
然而,一个解决方案是将 Javascript界面桥添加到 navWebView ,然后让将网址传回Java 然后Java可以通过 loadUrl 将其传递给第二个webView 。
JSInterface上的信息: http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29
答案 1 :(得分:-1)
JavaScriptHandler.java
public class JavaScriptHandler {
MainActivity parentActivity;
public JavaScriptHandler(MainActivity activity) {
parentActivity = activity;
}
public void setResultUrl(String url){
this.parentActivity.javascriptCallUrl(url);
}
}
MainActivity.java
// add line:
myNavbarView.addJavascriptInterface(new JavaScriptHandler(this), "MyHandler");
// Verwerk calls van navbar.html via JavaScriptHandler.java
public void javascriptCallUrl(final String url){
Log.v(TAG, "MainActivity JSHandler used: " + url);
// I need to run set operation of UI on the main thread.
// therefore, the above parameter "url" must be final
runOnUiThread(new Runnable() {
public void run() {
myWebView.loadUrl(url);
}
});
}
navbar.html
<a href="#" onclick="window.MyHandler.setResultUrl('file:///android_asset/HTML/index.html')">...</a>