我想为我的学校编程一个应用程序。因此,我想创建一个活动,在其中我可以显示学校主页的htm文件。我不希望应用程序打开浏览器,但应用程序应该在应用程序中显示文件的内容。我希望你明白我的意思。例如,如果有指向文件的链接:http://www.example.net/fileadmin/XYZ/Aktuelles/Schwarzes_Brett/Vertretungsplaene/Vertr-Pl-Mo.htm(别惊讶,我是德国人。)我该怎么办才能让它按我的意愿工作。谢谢你的帮助!:)
您可以使用WebView在本地或从任何url加载html文件。http://developer.android.com/reference/android/webkit/WebView.html
package com.plc.asnad.test;
import android.app.*;
import android.webkit.*;
import android.os.*;
import com.plc.asnad.*;
import java.net.*;
public class wvtutorial extends Activity
{
WebView wview = null; // 1 - Instance of Webview is initialize by null value
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// in lifecycle of vwtutorial activity and in OnCreate method , start to work
// with webview
// start to inflate and parsing layout xml , for access to view and viewgroups
// componnents
setContentView(R.layout.test);
// access to webview by casting the parsed views from layout xml
wview = (WebView)findViewById(R.id.mywview);
// create regular string for conatructing html body file
StringBuilder str = new StringBuilder();
str.append("<html>").append("n");
str.append("<head>").append("n");
str.append("<title>").append("mytest").append("<title>").append("n");
str.append("</head>").append("n");
str.append("<body>").append("n");
str.append("<b>hello webview</b>").append("n");
str.append("</body>").append("n");
str.append("</html>");
// set enable javascript feature of webview
wview.getSettings().setJavaScriptEnabled(true);
// finally, load html body stringed into wview and render and show it
wview.loadData(str.toString(),"text/html","utf-8");
// wview.loadurl("file:///android_assets/sample.html");
}
}
您可以使用本教程,它有一个java文件和一个布局XML:
http://uploadboy.me/wacaif2eagoj/wviewtutorial.zip.html