我有一个Android程序,可以在Web视图中显示HTML页面。 HTML页面存在于" asset / www / index.html"。
中我想在HTML页面上放置一个按钮,并在单击按钮时打开一个新活动。
这是Java代码:
public class HelloWebApp extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/www/index.html");
}
}
答案 0 :(得分:1)
您必须在html按钮点击时传递网址,如下所示
<强>的index.html 强>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<script type="text/javascript" charset="utf-8" src="jquery-2.0.0.min.js"></script>
<script type="text/javascript" charset="utf-8" src="quantize.js"></script>
<title>My HTML</title>
</head>
<body>
<h1>My HTML</h1>
<INPUT TYPE="button" value="Test" onClick="window.location='Navigation://OpenNativeScreen'">
</body>
</html>
现在,当按钮点击时,您将在网页视图的 shouldOverrideUrlLoading 方法中获取该网址。请参阅以下代码,
<强> MainActivity.java 强>
public class MainActivity extends Activity {
WebView myBrowser;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dropdown_html);
myBrowser = (WebView) findViewById(R.id.mybrowser);
myBrowser.setWebViewClient(new MyBrowser());
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser.loadUrl("file:///android_asset/www/index.html");
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals("Navigation://OpenNativeScreen")) {
startActivity(new Intent(MainActivity.this,SecondActivity.class));
finish();
return true;
}
return false;
}
}
}