Android Home Browser One can jump link and return

Source: Internet
Author: User

We will first make our own browser to do the following functions:
Click the link to jump to the page;
Be able to return to the original web page;
Can determine whether this is the first page to browse, if you press two times back key to exit.
Enter the URL via the input box to access the corresponding Web page.
We'll start with the partial code, and I'll publish the complete code at the end.
First load the Web page JavaScript must be able to support

        WebSettings ws = web.getSettings();        //是否允许脚本支持        ws.setJavaScriptEnabled(true);

Then we have to add webviewclient to WebView, only to load the Web page using our own WebView, remember View.loadurl (URL); This code must be, this is the code we click the link jump page

web.setWebViewClient(wvc);   WebViewClient wvc = new WebViewClient(){        public boolean shouldOverrideUrlLoading(WebView view, String url) {            view.loadUrl(url);            et_url.setText(url);            return false;        };    };

And this function, it is to prevent the input box entered the URL is not complete causes the page cannot be loaded

private void conn(String urlStr){        String url = "";        if(urlStr.contains("http://")){            url = urlStr;        }else{            url = "http://"+urlStr;        }        web.loadUrl(url);    }

There is the back key press two exits, in fact, this has a problem, if we jump to a webpage, and then press the Home button to return to the initial page, and then press the next time to quit, this I do not good, this can be recorded by pressing the back button two times, if the time is too short to quit , it's all right.
I'm here to show the functions of Web.cangoback () and Web.goback (), which are the first to be able to determine if there are any returned pages, and the second is to return to the previous page.

    public boolean onKeyDown(int keyCode, KeyEvent event) {        if(keyCode==KeyEvent.KEYCODE_BACK||web.canGoBack()){            web.goBack();            if(!web.canGoBack()){                if(isExit){                    return super.onKeyDown(keyCode, event);                }                isExit = true;                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();            }else{                isExit = false;            }        }        return true;    }

All right, we're done, let's complete the code.

public class Mainactivity extends Activity implements View.onclicklistener {private EditText et_url;    Private Button btn_visit;    Private WebView web;    Private Boolean isexit;    Private String homestr = "http://www.baidu.com"; -------------------------------------------public void OnCreate (Bundle savedinstancestate) {super.oncreate (s        Avedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.main);    Init (); }//-------------------------------------------/** * Basic Data initialization */private void init () {isexit = Fal        Se        Et_url = (EditText) Findviewbyid (R.id.et_url);        Et_url.settext (HOMESTR);        Btn_visit = (Button) Findviewbyid (r.id.btn_visit);        web = (WebView) Findviewbyid (R.id.web);        WebSettings ws = Web.getsettings ();        Whether scripts are allowed to support ws.setjavascriptenabled (true);        Web.setwebviewclient (WVC);        Btn_visit.setonclicklistener (this); Conn (Et_url. GetText (). toString ()); }//-------------------------------------------webviewclient WVC = new Webviewclient () {public Boolean Shoul            Doverrideurlloading (WebView view, String URL) {view.loadurl (URL);            Et_url.settext (URL);        return false;    };    }; -------------------------------------------/** * Access URL * @param urlstr */private void conn (String ur        lstr) {String url = "";        if (Urlstr.contains ("http://")) {url = urlstr;        }else{url = "http://" +URLSTR;    } web.loadurl (URL); }//-------------------------------------------public boolean onKeyDown (int keycode, keyevent event) {if (ke ycode==keyevent.keycode_back| |            Web.cangoback ()) {web.goback ();                if (!web.cangoback ()) {if (isexit) {return Super.onkeydown (KeyCode, event);                } Isexit = true; Toast.maketext (ThiS, "Press again to exit", Toast.length_short). Show ();            }else{isexit = false;    }} return true;            }//-------------------------------------------public void OnClick (View v) {switch (V.getid ()) {                Case R.id.btn_visit:conn (Et_url.gettext (). toString ());        Break }    }
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:orientation=" vertical "android:layout_width=" fill_parent "Android:layout_heigh        t= "Fill_parent" > <scrollview android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "0DP" android:layout_weight= "1" > <linearlayout android:orientatio            n= "vertical" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" > <linearlayout android:id= "@+id/layout" android:orientation= "Horizontal" an Droid:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_margin= "10DP" > <edittext android:id= "@+id/et_url" android:layout_width = "0DP" Android:layout_height= "Wrap_content" android:layout_weight= "1" android:inputtype= "Texturi"                    Android:singleline= "true" android:layout_margin= "10DP"/> <button Android:id= "@+id/btn_visit" android:layout_width= "Wrap_content" android:layou                    t_height= "Wrap_content" android:layout_margin= "10DP" android:text= "visit" Android:textsize= "15DP"/> </LinearLayout> <webview android:id= "        @+id/web "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "/> </LinearLayout> </ScrollView></LinearLayout>
<uses-permission android:name="android.permission.INTERNET"/>

Good bye

Android Home Browser One can jump link and return

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.