Original address: http://www.pocketdigi.com/20110216/176.html
WebView is a good thing, the equivalent of a mini browser, using the WebKit kernel, so the perfect support html,javascript,css and so on. Sometimes, we can completely put the UI and even data processing to WebView, with PHP and other service-side programs, so that Android development has become a web development, can save a lot of energy.
Here is a simple example of a webview, if you use all the functions to the server script processing, the program is very complete, you just write a good web page, the URL is filled, and then compiled, is a new software.
Program features: Open the page at the same time display a progressdialog, the page loading is hidden, click on the link on the page to display the ProgressDialog again, load the hidden, the return key can be returned to the previous page.
XML layout:
12345678910 |
<?xml version="1.0" encoding="UTF-8"?> <absolutelayout android:orientation="vertical" android:id="@+id/ Tab1 " android:layout_width=" Fill_parent " android:layout_height=" Fill_parent " xmlns:android="http://schemas.android.com/apk/res/android"> <webview android:id="@+id/wv" android:layout_width="Fill_parent" android:layout_height="Fill_parent" android:layout_x="0.0dip" android:layout_y="0.0dip" android:layout_weight="1.0" /> </AbsoluteLayout> |
Java code:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
Package Com.pocketdigi.webview;Import android.app.Activity;Import Android.app.AlertDialog;Import Android.app.ProgressDialog;Import Android.content.DialogInterface;Import Android.os.Bundle;Import Android.os.Handler;Import Android.os.Message;Import android.view.KeyEvent;Import android.webkit.WebChromeClient;Import Android.webkit.WebView;Import android.webkit.WebViewClient; Public classMainextendsActivity{ /** Called when the activity is first created. * /WebView WV;ProgressDialog PD;Handler Handler;@Override Public voidOnCreate(Bundle savedinstancestate) { Super.onCreate(Savedinstancestate);Setcontentview(R.Layout.Main);Init();//Execute initialization functionLoadurl(Wv"Http://www.pocketdigi.com");Handler=NewHandler(){ Public voidHandlemessage(Message msg) {//Define a handler to handle the communication between the download thread and the UI if (!Thread.CurrentThread().isinterrupted()) { Switch (Msg. What) { Case 0:Pd.Show();//Show progress dialog box Break; Case 1:Pd.Hide();//Hide Progress dialog box, do not use dismiss (), cancel (), or call Show () again, the small circle of the displayed dialog will not move. Break; } } Super.Handlemessage(Msg); } }; } Public voidInit(){//InitializeWv=(WebView)Findviewbyid(R.ID.WV);Wv.getsettings().setjavascriptenabled(true);//JS availableWv.Setscrollbarstyle(0);//scroll bar style, 0 is not to leave the scroll bar space, scroll bar overlay on the pageWv.setwebviewclient(NewWebviewclient(){ Public BooleanShouldoverrideurlloading(FinalWebView View,Final StringUrl) {Loadurl(View,url);//Loading Web pages return true; }//Rewrite click action, load with WebView});Wv.setwebchromeclient(NewWebchromeclient(){ Public voidOnprogresschanged(WebView View,intProgress){//Load progress changes and trigger if(Progress== -){Handler.Sendemptymessage(1);//If all loads, hides the progress dialog box } Super.onprogresschanged(View, Progress); } });Pd=NewProgressDialog(Main. This);Pd.Setprogressstyle(ProgressDialog.Style_spinner);Pd.Setmessage("Please wait while the data is loaded!" "); } Public BooleanOnKeyDown(intKeyCode,KeyEventEvent) {//Snap back key if ((KeyCode== KeyEvent.Keycode_back) &&Wv.CanGoBack()) {Wv.GoBack(); return true; }Else if(KeyCode== KeyEvent.Keycode_back){Confirmexit();//Press the return key, but cannot return, execute exit confirmation return true; } return Super.OnKeyDown(KeyCode, Event); } Public voidConfirmexit(){//Exit ConfirmationAlertdialog.BuilderAd=NewAlertdialog.Builder(Main. This);Ad.Settitle("Exit");Ad.Setmessage("Do you want to quit the software?");Ad.Setpositivebutton("Yes",NewDialoginterface.Onclicklistener() {//Exit button@Override Public voidOnClick(Dialoginterface Dialog,intI) {//TODO auto-generated method stubMain. This.Finish();//Close activity}});Ad.Setnegativebutton("No",NewDialoginterface.Onclicklistener() {@Override Public voidOnClick(Dialoginterface Dialog,intI) {//Do not exit without performing any action}});Ad.Show();//Display dialog box } Public voidLoadurl(FinalWebView View,Final StringUrl){ New Thread(){ Public voidRun(){Handler.Sendemptymessage(0);View.Loadurl(Url);//Loading Web pages } }.Start(); }} |