1, WebView display progress bar in the OnCreate event write:
[Java]View Plaincopy
- WebView Mywebview = (WebView) Findviewbyid (R.ID.WEBVIEW1);
- Final activity activity = this;
- Mywebview.setwebchromeclient (New Webchromeclient () {
- public void onprogresschanged (WebView view, int progress) {
- Activity.settitle ("Trying to load for you ....");
- Activity.setprogress (Progress * 100);
- if (progress = = 100)
- Activity.settitle ("completed");//or set as the original activity title
- }
- });
2, Web page adaptive webview width in the OnCreate event wrote:
[Java]View Plaincopy
- WebView Mywebview = (WebView) Findviewbyid (R.ID.WEBVIEW1);
- Mywebview.getsettings (). Setlayoutalgorithm (Layoutalgorithm.single_column);
3, WebView support Web JavaScript language in the OnCreate event write:
[Java]View Plaincopy
- WebView Mywebview = (WebView) Findviewbyid (R.ID.WEBVIEW1);
- Mywebview.getsettings (). Setjavascriptenabled (True);
4, the page inside the hyperlink in WebView in Riga, not in the phone default browser opened in the OnCreate event wrote:
[Java]View Plaincopy
- WebView Mywebview = (WebView) Findviewbyid (R.ID.WEBVIEW1);
[Java]View Plaincopy
- Mywebview.setwebviewclient (New Webviewclient () {
- public boolean shouldoverrideurlloading (WebView view, String URL) {
- View.loadurl (URL);
- return true;
- }
- });
[Java]View Plaincopy
- Mywebview.loadurl ("http://www.baidu.com"));//The default load URL can only be placed behind setwebviewclient otherwise hyperlinks in the Web page or in the phone's default browser open instead of in the WebView in Riga
5, press the Phone Back button back page instead of opening the WebView program before the interface:
[Java]View Plaincopy
- public boolean onKeyDown (int keycode, keyevent event)
- {//Check if the key event is the back key and if there's history
- WebView Mywebview = (WebView) Findviewbyid (R.ID.WEBVIEW1);
- if ((keycode = = keyevent.keycode_back) && mywebview.cangoback ())
- {
- Mywebview.goback ();
- return true;
- }//If It wasn ' t the back key or there's no Web page history, bubble up to the default//system behavior (probably exit The activity)
- Return Super.onkeydown (KeyCode, event);
- }
6, Web page JS call Android Program:
New Class of Javascriptinterface
[Java]View Plaincopy
- Package com.example.androidhouse;
- Import Android.content.Context;
- Import Android.widget.Toast;
- public class Javascriptinterface {
- Context Mcontext;
- /** instantiate the interface and set the context */
- Javascriptinterface (Context c) {mcontext = C;}
- /** Show A toast from the Web page */
- public void Showtoast (String toast)
- {
- Toast.maketext (Mcontext, Toast, Toast.length_short). Show ();
- }
- }
In the OnCreate event, write:
[Java]View Plaincopy
- WebView Mywebview = (WebView) Findviewbyid (R.ID.WEBVIEW1);
[Java]View Plaincopy
- Mywebview.addjavascriptinterface (New Javascriptinterface (This), "Android");
The page JS code is written in:
[JavaScript]View Plaincopy
- Window. Andorid.showtoast ("I Call the Android program");
Remember: in Andorid2.2 and Android2.3 inside the WebView JS call will cause a system crash VM aborting error This is the two version of the bug solved very troublesome until now, I still did not find a solution.