Android webview usage Summary 1

Source: Internet
Author: User

Share common items in webview

To load a page with webview, just webview. loadurl ("http://m.baidu.com"); of course don't forget to add network Permissions

Click the hyperlink in the webpage. multiple browsers will pop up, but to open it in our browser, we need to set webviewclient

webView.setWebViewClient(new WebViewClient(){}

Processing of the Return key: After opening multiple webpages, we click back to return to the previous webpage, but in fact the current activity will be closed, so we need to process the return key.

@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {webView.goBack();return true;} elsereturn super.onKeyDown(keyCode, event);}

Here is the difference between return true and return false.

When true is returned, it indicates that the event has been fully executed and no other callback functions are executed. If false is returned, it indicates that the event is not fully processed and other callback functions will be called.

I just talked about moving backwards. How can I proceed?

Run the following command when the forward button is clicked.

If (webview. cangoforward () {webview. goforward ();} else {toast ("this is the last page, so you cannot move on again ");}

Run the command when the refresh button is clicked.

webView.reload();

During page loading, we may need to give users a friendly prompt, which is also done in webviewclient ().


Webview. setwebviewclient (New webviewclient () {@ overridepublic void onpagestarted (webview view, string URL, bitmap favicon) {If (progdlg = NULL |! Progdlg. isshowing () {progdlg = new progressdiwing (CTX); progdlg. setmessage ("loading... ") ;}Progdlg. Show () ;}@ overridepublic void onpagefinished (webview view, string URL) {progdlg. Dismiss ();}}

To know the loading progress during page loading, you need to use another class webchromeclient.

webView.setWebChromeClient(new WebChromeClient(){@Overridepublic void onProgressChanged(WebView view, int newProgress) {Message msg = new Message();                                msg.what = 200;                                msg.obj = newProgress;                                handler.sendMessage(msg);}}

Update the progress bar in 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;}};};

After loading, let the progressbar disappear.

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.