在佈局文件中添加一個webview。使用下面的代碼加載URL
WebView web = (WebView) findViewById(R.id.web);
ProgressDialog progressBar = new ProgressDialog(Activity.this);
progressBar.setMessage("Loading");
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.show();
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView webView, int errorCode,
String description, String failingUrl) {
super.onReceivedError(webView, errorCode, description, failingUrl);
try {
webView.stopLoading();
} catch (Exception e) {//
}
if (webView.canGoBack()) {
webView.goBack();
}
AlertDialog alertDialog = new AlertDialog.Builder(Activity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Cannot connect to the Server." +
" Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
"Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
if (progressBar.isShowing()) {
progressBar.dismiss();
}
alertDialog.show();
}
});
web.loadUrl("your_url");
你能發佈一些代碼嗎? – user3641702