A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.
loadDataWithBaseURL(baseUrl==null/"about:blank",……),baseUrl为null或"about:blank"指定相对路径.
WebView 主要调用三个方法:LoadUrl、LoadData、LoadDataWithBaseURL.
1、LoadUrl 直接加载网页、图片并显示.(本地或是网络上的网页、图片、gif)
2、LoadData 显示文字与图片内容 (模拟器1.5、1.6)
3、LoadDataWithBase 显示文字与图片内容(支持多个模拟器版本)
我的代码:
public class WebViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView webView = new WebView(this);
webView = (WebView) findViewById(R.id.webView);
// 如果访问的页面中有Javascript,则webview必须设置支持Javascript。
webView.getSettings().setJavaScriptEnabled(true);
// 触摸焦点起作用
// requestFocus();//如果不设置,则在点击网页文本输入框时,不能弹出软键盘及不响应其他的一些事件。
// 取消滚动条
// this.setScrollBarStyle();
// 设置WevView要显示的网页:
// 互联网用:
webView.loadUrl("http://write.blog.csdn.net/postedit");
// 本地文件用:webView.loadUrl("file:///android_asset/XX.html");// 本地文件存放在:assets文件中
webView.setWebViewClient(new webViewClient());
}
//监听器
class webViewClient extends WebViewClient{
/**
* 对网页中超链接按钮的响应
* 当按下某个连接时WebViewClient会调用这个方法,并传递参数:按下的url
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
// TODO Auto-generated method stub
super.onReceivedHttpAuthRequest(view, handler, host, realm);
}
}
}
别人的代码:
" + | " + " + vNum + |