在assets新建一个demo.html 文件。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>面试帮Demo</title>
</head>
<body style="font-size: 20px;">
<ul>
<li><a href="testapp://test?id=100101&title=HelloAndroid">Start
TestActivity</a></li>
</ul>
</body>
</html>
在XML布局中创建一个WebView控件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
在MainActivity代码中调用。
public class MainActivity extends Activity { private WebView mWebView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWebView(); mWebView.loadUrl("file:///android_asset/demo.html");
} private void initWebView() {
mWebView = (WebView) findViewById(R.id.web);
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
setTitle(title);
}
});
} }
传值到另外一个Activity接收。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); TextView tv = new TextView(this);
setContentView(tv); Uri data = getIntent().getData();
if (data != null) {
String id = data.getQueryParameter("id");
String title = data.getQueryParameter("title");
String text = "id=" + id + ", title=" + title;
tv.setText(text);
}
}