Android implements its own browser

Source: Internet
Author: User

Recently, I had nothing to do with writing a simple browser. At the beginning, I encountered some problems. The main problem was how to display all the webpage data in my webview, however, if you do not specify your own webview to display all webpage data, the system will call the default webview to load the data. Therefore, you can use the setwebviewclient () method of webview to set your own webviewclient object, this object is a class specifically responsible for binding webpage URLs to webview. Rewrite the shouldoverrideurlloading (webview view, string URL) method in the class and change the view to its own webview.

When you press the rollback keyboard, you can return to the history page and rewrite the onkeydown () method of the activity to intercept the rollback key event and then determine webview. cangoback (), returns true to webview. just Goback.

If you want to add the zoom-in/zoom-in control button, you must first set that webview supports zoomcontrols by using web_content.getsettings (). setsuppzoom zoom (true), and then use webview. getzoomcontrols () method to get the view of zoomcontrols, and then getwindow (). getdecorview (). findviewbyid (Android. r. id. content) to get the space of the displayed content of webview. Now, you only need to add the obtained zoomcontrols to the content space of webview. Another problem is that when the edittext space appears above the webview, if the webview also appears at the same time (such as the input boxes in Baidu and Google), the focal point conflict will occur, the solution is to implement the setontouchlistener event of webview and set webview in the event. requestfocus.

Paste the source code:

Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> Init (); <br/>}< br/> private void Init () {<br/> web_content = (webview) findviewbyid (R. id. web_content); <br/> websettings set = web_content.getsettings (); <br/> set. setsuppzoom zoom (true); <br/> set. setjavascriptenabled (true); <br/> set. setcachemode (websettings. load_cache_else_network); </P> <p> View control = web_content.getzoomcontrols (); <br/> framelayout layout = (framelayout) getwindow (). getdecorview () <br/>. findviewbyid (Android. r. id. content); <br/> framelayout. layoutparams Params = new framelayout. layoutparams (<br/> framelayout. layoutparams. wrap_content, <br/> framelayout. layoutparams. wrap_content, gravity. bottom <br/> | gravity. right); <br/> Params. bottommargin = 10; <br/> Params. rightmargin = 10; <br/> layout. addview (control, Params); </P> <p> web_content.setontouchlistener (new view. ontouchlistener () {</P> <p> @ override <br/> Public Boolean ontouch (view V, motionevent event) {<br/> web_content.requestfocus (); <br/> return false; <br/>}< br/>}); </P> <p> web_content.setwebviewclient (New webviewclient () {</P> <p> @ override <br/> Public Boolean shouldoverrideurlloading (webview view, string URL) {<br/> View. loadurl (URL); <br/> tx_address.settext (URL); <br/> return true; <br/>}</P> <p> }); <br/> web_content.loadurl (parseurl (defaultpage); <br/>}

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.