2011年9月28日 星期三

About DIII Game Guide App WebView

Xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<ProgressBar android:id="@+id/progress_web_load"
style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent"
android:layout_height="10dip" android:max="100" android:progress="0" />
<WebView android:id="@+id/webview"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>

ProgressBar 用來顯示網頁讀取進度

Java:

public class Web extends Activity {
private WebView wv;
private ProgressBar pgWebLoad;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
wv = (WebView) findViewById(R.id.webview);
pb = (ProgressBar) findViewById(R.id.progress_web_load);

wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setSavePassword(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setPluginsEnabled(true); //true才能顯示flash


if (this.getIntent().getBooleanExtra("isDesktop", false)) {
wv.getSettings().setUserAgentString(
"Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/11.04 Chromium/12.0.742.91 Chrome/12.0.742.91 Safari/534.30");
}
//是否強制以桌面瀏覽器呈現方式

wv.setFocusable(true);
wv.setFocusableInTouchMode(true);
wv.setWebChromeClient(new WebChromeClient() {

@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
pb.setProgress(newProgress);
if (newProgress == 100) {
pb.setVisibility(View.GONE);
} else {
pb.setVisibility(View.VISIBLE);
}
super.onProgressChanged(view, newProgress);
}
});

wv.setWebViewClient(new WebViewClient() {

public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(WebSite.this, description, Toast.LENGTH_SHORT).show();
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
});
wv.loadUrl(getIntent().getStringExtra("url"));
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
wv.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

沒有留言:

張貼留言