Basic use of WEBVEIW

Source: Internet
Author: User

Today, that's easy to understand. WebView

It is broadly divided into two situations: 1 in fragment 2 in activity

1 WebView Client Click Fallback in fragment is a challenge


Workaround for fallback code:

The principle is very simple: is to listen to Fragment current event, but press the time, see is not the fallback key, WebView can fallback?

If you can, just go back!

Set WebView internal fallback jump: Webview.setonkeylistener (New Onkeylistener () {@Overridepublic Boolean onKey (View v, int keycode, KeyEvent event) {//TODO auto-generated method Stubif (event.getaction () = = Keyevent.action_down) {if (keycode = = Keyevent.keycode_back && Webview.cangoback ()) {webview.goback (); return true;}} return false;}});



--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------



Package Com.cim.pd.ui.fragment;import Android.app.activity;import Android.os.bundle;import Android.support.annotation.nullable;import Android.support.v4.app.fragment;import Android.view.KeyEvent;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.view.onkeylistener;import Android.view.viewgroup;import Android.webkit.webview;import Android.widget.imageview;import Android.widget.progressbar;import Com.cim.pd.r;import com.cim.pd.ui.MainActivity; Import com.cim.pd.view.mywebviewclient;/** * menu bar: * Buy the Lexmark Tracker page * * @author Hades * @Time 2014-12-4 * */public class Buyde  Vicefragment extends Fragment {public static final String ARG = "Buydevicefragment";p rivate mainactivity mainactivity;/** * Purchase Equipment WebView * */private WebView webview;/** * progress bar * */private ProgressBar ProgressBar; @Overridepublic void Onattach (Ac Tivity activity) {Super.onattach (activity); mainactivity = (mainactivity) activity;} @Overridepublic void OnCreate (Bundle sAvedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate);} @Overridepublic View Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle Savedinstancestate) {//TODO auto-generated method Stubview view = Inflater.inflate (R.layout.fragment_buy_device, container, false); Inittop (view); Initwebview (view); return view;} /** * Initialize the information at the top * * @author Hades * @Time 2014-12-4 * @param view */private void Inittop (view view) {ProgressBar = (Progres SBar) View.findviewbyid (R.id.buy_progressbar);p rogressbar.setvisibility (view.visible); ImageView Drawerview = ( ImageView) View.findviewbyid (r.id.drawer_menu);//Set Listener: Drawerview.setonclicklistener (New Onclicklistener () {@ overridepublic void OnClick (View v) {//TODO auto-generated method Stubmainactivity.toggle ();}});} /** * Initialize page information: * * @author Hades * @Time 2014-12-4 * @param view */private void Initwebview (view view) {WebView = (webView ) View.findviewbyid (R.id.buy_webview); Webview.getsettings (). SetjavasCriptenabled (TRUE); String url = "http://m.cim120.com/";//First step: Directly load URL webview.loadurl (URL);//Second step: Set up the page to jump in the current WebView! Mywebviewclient client = new Mywebviewclient (Mainactivity,progressbar); webview.setwebviewclient (client);//settings WebView internal fallback Jump: Webview.setonkeylistener (New Onkeylistener () {@Overridepublic Boolean onKey (View v, int keycode, KeyEvent event) {//TODO auto-generated method Stubif (event.getaction () = = Keyevent.action_down) {if (keycode = = Keyevent.keycode_back && Webview.cangoback ()) {webview.goback (); return true;}}}); @Overridepublic void Ondestroyview () {//TODO auto-generated method Stubsuper.ondestroyview ();}}


2 Activity

/** * Initialize Component *  * @Hades * @Dec 3, * @11:35:35 PM */private void init () {WebView = (WebView) This.findviewbyid (r.id . My_buy_webview); Webview.getsettings (). Setjavascriptenabled (True); String url = "http://m.cim120.com/";//The first way: Directly load webview.loadurl (URL);//second: Create your own  browser! Search the data of the friend in the inside display! Mywebviewclient client = new Mywebviewclient (Buydeviceactivity.this,progressbar); webview.setwebviewclient (client);} /** * Set webviewclient internal fallback */@Overridepublic void onbackpressed () {//TODO auto-generated method Stubif ( Webview.cangoback ()) {Webview.goback ();} Else{finish ();}}

There is a callback method inside the Activity that listens for the fallback key.

onbackpressed;


--------------------------------------------------------------------------------------------------------------- ------------------

WebView in Webview.getsettings (). Setjavascriptenabled (True);


This is added Js support, if there is no such sentence, it will lead to webView loading the page,
JS effects, can not be displayed!


--------------------------------------------------------------------------------------------------------------- ----------------

Some of the little ideas today:

When jumping to buydeviceactivity


When you click, how do you save the data?
Error:
12-04 13:51:19.778:e/crashhandler (19281): caused By:libcore.io.ErrnoException:write failed:enospc (No space left on de Vice


The reason is because: I was too careless! Not added in application register activity!


-------------------------------------------------------------------------------- ----------------------------------------

Webviewclient is the data used to display the contents of the WebView;

If you do not have him, when you click Ah webview internal content, it will default to open a intent to call the local

Browser's app.

Therefore, the webviewclient must be customized, and shouldoverrideurlloading must be rewritten.


Package Com.example.webview;import Android.app.activity;import Android.content.context;import Android.util.attributeset;import Android.view.view;import Android.webkit.webview;import Android.webkit.webviewclient;import Android.widget.progressbar;public class Mywebviewclient extends WebViewClient { Private ProgressBar progressbar;public mywebviewclient () {//TODO auto-generated constructor Stub}public Mywebviewclient (Activity context) {//TODO auto-generated constructor Stubsuper ();} Public mywebviewclient (Activity Context,progressbar ProgressBar) {//TODO auto-generated constructor Stubsuper (); This.progressbar = ProgressBar;} @Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {//TODO auto-generated method Stubview.loadurl (URL); return true;} /** * WebView Load Complete data * */@Overridepublic void onpagefinished (WebView view, String URL) {//TODO auto-generated method stub/ /super.onpagefinished (view, URL);p rogressbar.setvisibility (View.gone);}}

Onpagefinished () is called after the WebView has been successfully loaded,

Here you can set the progress bar to gone!

Let the progress bar disappear.


Basic use of WEBVEIW

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.