如何创建webview,先看Activity方法:
public class Hello extends Activity {
/** Called when the activity is first created. */
private WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webview= new WebView(this);
setContentView(webview);
webview.loadUrl("http://www.imf7.com/webapp/");
//设置支持JavaScript脚本
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
//设置可以访问文件
webSettings.setAllowFileAccess(true);
//设置不支持缩放
webSettings.setBuiltInZoomControls(false);
//设置滚动条样式,去掉滚动条白边
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
//设置WebViewClient
// 在WebView中而不是默认浏览器中显示页面 取消地址栏
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
});
}
//设置回退
//覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
@Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
}
另外,AndroidManifest.xml中必须添加<uses-permission android:name="android.permission.INTERNET" />进行许可,放在<uses-sdk android:minSdkVersion="8" />下面就行。