Android to achieve a simple browser problem solving method _android

Source: Internet
Author: User

Recently have nothing to write a simple browser, at the beginning of writing encountered some problems, the main problem is how to display all the data in their own WebView Web page, But do not specify their own webview display all Web page data will call the system default WebView to load the data, so the solution is to use the WebView setwebviewclient () method to set up their own Webviewclient object, which is specifically responsible for the Web page URL and webview binding classes, rewrite the shouldoverrideurlloading ( WebView view, String url) method, change view to own WebView OK.

When you press the rollback keyboard to return to the history page you can override the OnKeyDown () method of the activity to intercept the fallback key event, and then Judge Webview.cangoback () and return True to Webview.goback ().

If you want to add the Zoom/Drop control button must first set WebView is to support Zoomcontrols, set the method is through Web_content.getsettings (). Setsupportzoom (True), The view of Zoomcontrols is then obtained through the Webview.getzoomcontrols () method, and then through GetWindow (). Getdecorview (). Findviewbyid ( android.r.id.content) Get WebView display content space, now just get Zoomcontrols added to the WebView content space in the OK. There are 1 other problems is when the webview edittext space, if the webview also appear (such as: Baidu, Google in the input box) will appear focus conflict problems, The solution is to implement the WebView Setontouchlistener event and set Webview.requestfocus () in the event.

or paste the source code bar:

public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.main); 
  Init (); 
private void Init () {web_content = (WebView) Findviewbyid (r.id.web_content); 
    WebSettings set = Web_content.getsettings (); 
    Set.setsupportzoom (TRUE); 
    Set.setjavascriptenabled (TRUE); 
 
    Set.setcachemode (websettings.load_cache_else_network); 
    View control = Web_content.getzoomcontrols (); Framelayout layout = (framelayout) GetWindow (). Getdecorview (). Findviewbyid (Android. 
    R.id.content); Framelayout.layoutparams params = new Framelayout.layoutparams (FrameLayout.LayoutParams.WRAP_CONTENT, F RameLayout.LayoutParams.WRAP_CONTENT, Gravity.bottom | 
    Gravity.right); 
    Params.bottommargin = 10; 
    Params.rightmargin = 10; 
 
    Layout.addview (control, params); Web_content.setontouchlistener (New View.ontouchlistener () {@Override public boolean Ontouch ViewV, motionevent event) {Web_content.requestfocus (); 
      return false; 
 
    } 
    }); Web_content.setwebviewclient (New Webviewclient () {@Override public boolean shouldoverrideurlloading (WEBVI 
        ew view, String URL) {view.loadurl (URL); 
        Tx_address.settext (URL); 
      return true; 
} 
 
    }); 
Web_content.loadurl (parseURL (defaultpage));  }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.