Android Development's WebView use advanced

Source: Internet
Author: User

1. When you open a hyperlink on a Web page that is displayed on WebView, the browser of the system is invoked to open instead of the original WebView display.
2, when I press the return key, is not the return of the last open page, but directly exit the program. Here's how to solve the two problems above

Displaying an open link on the original webview requires customizing a class, inheriting from Webviewclient, and setting WebView webviewclient:

  code is as follows copy code
class Mywebviewclient extends Webviewclient
{
   public boolean shouldoverrideurlloading (WebView view, Stri ng URL)
   {
      //Overriding this method indicates that clicking the link is a jump in the current WebView and does not jump to the browser
        view.loadurl (URL);
       return true;
  }
}
 
WebView = (webview) This.findviewbyid (R.id.webview);
 
Mywebviewclient MWVC = new Mywebviewclient ();
Webview.setwebviewclient (MWVC);
 
Webview.loadurl ("/index.php");


This way, when you click on a hyperlink in a Web page, it is displayed in the current webview instead of opening the browser to display the page.

Response return key back to previous page after the first problem is solved, a new problem is raised, that is, when you click on several layers of links, the return button is not rolled back to the previous page as expected, but simply exits the program.

To solve this problem, you need to rewrite the onkeydown method, when you press the return key to determine whether the currently open page is a top-level page, if it is, then quit the activity, if not, then return to the previous page:

The code is as follows Copy Code

String Toppage = "/index.php";
Public boolean onKeyDown (int keycode, keyevent event)
{
   if (keycode = = Keyevent.keycode_back)
   {
      String currenturl = Webview.geturl ();
      if (currenturl.equals (toppage))
      {
 & nbsp;        This.finish ();
     }
      Else
      {
           Webview.goback ();
     }
      return true;
  }
   return false;
}

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.