About Android WebView

Source: Internet
Author: User

Prior to WebView's understanding is very superficial, this time just study:

  1. When loading URLs in WebView, use Load.url (""), but remember to configure them in Androidmanifest.xml. The configuration statement is: <uses-permission android:name= "Android.permission.INTERNET"/>

  2. Overloaded shouldoverrideurlloading (WebView view, String URL), when linked at the time, is loaded in the software instead of jumping to the browser.

  3. Processing of the return key: After we have opened multiple pages, we click Return to return to the previous page, but in fact it will close the current activity, so we have to handle the return key.

    @Override

    Public boolean onKeyDown (int keycode, keyevent event) {

    if (KeyCode = = Keyevent.keycode_back && webview.cangoback ()) {

    Webview.goback ();

    return true;

    } Else

    return Super. OnKeyDown (KeyCode, event);

    }

  4. Page Forward button.

    if (Webview.cangoforward ()) {

    Webview.goforward ();

    }Else{

    Toast ("It's the last page, no more Forward");

    }

  5. When you click the Refresh button, execute webview.reload ();

  6. If you want to add a message when the page loads, you can make a WebClient

    Onpagestarted (WebView view, String URL, Bitmap favicon) {

    }

    Onpagefinished (WebView view, String URL) {}

    Example:

    @Override  
                public void Onpagestarted (WebView view, String URL, Bitmap favicon) { 
            & nbsp;       if (Progdlg = = NULL | |!progdlg.isshowing ()) { 
                         Progdlg = new ProgressDialog (CTX); 
                        progdlg.setmessage ("Loading, please wait ... "); 
                   } 
                   progdlg.show (); 
               } 

@Override
public void onpagefinished (WebView view, String URL) {
Progdlg.dismiss ();
}

7. If you want to know the loading progress, you need to call another class webchromeclient.

Example:

Webview.setwebchromeclient (New Webchromeclient () {

@Override
public void onprogresschanged (WebView view, int newprogress) {
Message msg = new Message ();
Msg.what = 200;
Msg.obj = newprogress;
Handler.sendmessage (msg);
}
}

Then update the progress bar in the handler.

Private Handler Handler = new Handler () {
public void Handlemessage (Android.os.Message msg) {
Switch (msg.what) {
Case 200:
int progress = (Integer) msg.obj;
Progressbar.setprogress (progress);
Break

Default
Break
}
};
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.