Android WebView巢狀Html 之一
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("
// 本地檔案用: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);
}
}
}
別人的程式碼:
- public class AboutView extends Activity implements OnTouchListener,
- OnClickListener {
- private Button btnAbout;
- private WebView aboutBrowser;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.about);
- btnAbout = (Button) this.findViewById(R.id.about_button);
- aboutBrowser = (WebView) this.findViewById(R.id.about_webkit);
- btnAbout.setOnTouchListener(this);
- btnAbout.setOnClickListener(this);
- // WebSettings s = aboutBrowser.getSettings();
- // s.setUseWideViewPort(true);
- // s.setJavaScriptEnabled(true);
- // s.setBlockNetworkImage(true);
- loadHTML();
- }
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- return false;
- }
- @Override
- public void onClick(View v) {
- if(v == btnAbout) {
- //這裡是切換Activity
- //注意:每個Activity都要在AndroidManifest.xml中註冊
- Intent i = new Intent();
- i.setClass(AboutView.this, CiHaiView.class);
- this.startActivity(i);
- AboutView.this.finish();
- }
- }
- public void loadHTML() {
- String vStr = getText(R.string.vStr).toString();
- String vNum = getText(R.string.vNum).toString();
- final String htmlText = "<html>" +
- "<head></head>" +
- "<body>" +
- "<br/>" +
- "<table align=\"center\" border=\"0\" cellspacing=\"15\">" +
- "<tr align=\"center\"><td><img src=\"file:///android_asset/edict.png\" /></td>" +
- "<td>" +
- "<b><font size=\"5\">" + vStr +"</font></b><br/>" + vNum +
- "</td></tr>" +
- "</table>" +
- " " +
- "</body></html>";
- aboutBrowser.loadDataWithBaseURL("about:blank", htmlText, "text/html", "utf-8", null);
- }
- }