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; } |