Android WebView Development FAQs

Source: Internet
Author: User

1, add permissions: Androidmanifest.xml must use the license "Android.permission.INTERNET", otherwise the Web page is not available error.
2. Generate a WebView component in the activity: WebView WebView = new WebView (this);
3, set WebView basic information:
If you have JavaScript on the page you are visiting, WebView must set up support JavaScript.
Webview.getsettings (). Setjavascriptenabled (True);
Touch Focus Works
Requestfocus ();//If not set, you cannot eject the soft keyboard and do not respond to other events when you click on the text input box of the Web page.
Cancel scroll bar
This.setscrollbarstyle (Scrollbars_outside_overlay);

4. Set Wevview to display the page:

Internet use: Webview.loadurl ("http://www ...");
For local files: Webview.loadurl ("file:///android_asset/XX.html"); Local files are stored in the: Assets file


5. If you want to click on the link handled by yourself, instead of the new Android system browser should link.
Add an Event Listener object (webviewclient) to WebView and override some of these methods shouldoverrideurlloading: Responses to hyperlink buttons in a Web page.

Webviewclient calls this method when a connection is pressed and passes a parameter: The URL that is pressed

Java code:

Onloadresourceonpagestartonpagefinishonreceiveerror0onreceivedhttpauthrequest


6, if the WebView point link to see a lot of pages later, if you do not do any processing, click the System "back" button, the entire browser will call finish () and end itself, if you want to browse the page fallback instead of exiting the browser, The back event needs to be processed and consumed in the current activity.
Overrides the onkeydown (int keycoder,keyevent event) method of the Activity class.

public boolean onKeyDown (int keycoder,keyevent event) {if (Webview.cangoback () && Keycoder = = Keyevent.keycode_ back) {webview.goback ();//goback () represents the return of the previous page of WebView return true;} return false;}


Android WebView is very powerful, is actually a browser, you can embed it in the location you want, I have two questions here, is how to know the page loading progress and load the page, click on the link inside the page or in the current WebView jump, do not want to jump to the browser side, The solution is as follows:

This method can handle webview at load time and when loading is complete webview.setwebchromeclient (new Webchromeclient () {@Overridepublic void Onprogresschanged (WebView view, int newprogress) {if (newprogress==100) {//Here is the caption for setting activity, You can also do some other operations according to your needs Title.settext ("Loading Complete");}else{title.settext ("Loading .....");}}); Webview.setwebviewclient (New Webviewclient () {@Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {//Override this method to indicate that clicking on a link in a webpage or jumping in the current webview does not jump to the browser view.loadurl (URL); return true;} @Overridepublic void Onreceivedsslerror (WebView view, Sslerrorhandler handler, android.net.http.SslError error) {// Override this method to allow WebView to handle HTTPS requests handler.proceed ();});


Related Article

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.