GitHub Project Parsing (vii)-Prevent button repeat click

Source: Internet
Author: User

Reprint please indicate the source: a column of Maple Leaf

In this article I will describe a small library of tools that I have Encapsulated: button click event Class Library.

function:
This kind of library can prevent the button repeated click, can judge the network state, can judge the user login status, as well as the custom authentication condition and so on.

Description:
The core principle of its implementation is to implement its own Onclicklistener class by customizing, and override the OnClick method in it, and then callback our custom abstract method after executing the corresponding judgment logic in the OnClick method.

The exact effect is as shown:

How to use

    • Block Multiple Click events
/**  * 测试快速点击事件  */ fastButton.setOnClickListener(new OnClickFastListener() {     /**      * 自定义实现的抽象方法,不在重写onClick方法,该方法用于执行点击后的逻辑      */      @Override     publicvoidonFastClick(View v) {         "您点击了测试网络按钮...", Toast.LENGTH_LONG).show();     } });

The Fastbutton here is a normal button component, then we set a click event for the component, and we pass in our custom Onclicklistener class (here, it is necessary to note that not just the button component of any view component of the Click event is OK, Here is just a button component for example), where the default mask multiple click event interval is 900ms, that is, when we set our own custom Click event Listener for the component, if there are two click events, And the second click if the distance from the first click event is less than 0.9s, then the second click will not work;

    • Shielding network conditions
/** * Test Network status */ Networkbutton.setonclicklistener (new  Onclicknetworklistener () {/** * Custom implementation of the abstract method, mainly used to perform the network after the logic */  @Override  public  void   Onnetworkclick  (View v) {toast.maketext (Mcontext,  "current device has network, perform subsequent operations ..." , Toas    T.length_long). Show (); } /** * Custom implementation of the abstract method, mainly for the execution of the current device after the network logic */ @o Verride  public  void  onnonetworkclick  (View v) {toast.maketext (Mcontext, " current Device no network ... ", Toast.length_long). Show (); }});

Similarly, the Networkbutton here is a custom button component that we set up for the Click event Listener and passed in our custom Onnetworkclicklistener class, You can find that there are two callback methods in the Onnetworkclicklistener class, where the Onnetworkclick method is used to perform the current device network, and the Onnonetworkclick method is used to handle the subsequent operation of the current device without network;

    • Masking whether to log in
/** * Test whether to login */Loginbutton.setonclicklistener (NewOnclickloginedlistener (Mcontext) {/** * Custom Implementation abstract method to determine if the current device is logged in */    @Override     Public Boolean islogined(Activity context, view view) {return false; }/** * is primarily used to perform logic that determines the execution of a user after login * /    @Override     Public void Onloginedclick(View v) {Toast.maketext (Mcontext,"Device is logged in, then follow up ...", Toast.length_long). Show (); }/** * is primarily used to perform logic that determines when a user is not logged in.    @Override     Public void Onnologinedclick(View v) {Toast.maketext (Mcontext,"The device is not logged in and cannot perform subsequent operations ...", Toast.length_long). Show (); }});

The Loginbutton is also a button component of our custom, and we set our Onloginedclicklistener class, and then there are three callback methods,
Where the method islogined is used to determine whether the current user is logged on, return to TRUE indicates that the user is logged in, return to False indicates that the user is not logged in, the implementation logic needs to be implemented at the business level, the method Onloginedclick used to execute the logic after login, The method Onnologinedclick is used to execute the logic that the user has not logged in.

This is the approximate implementation of this class library function, after the completion of the function, we look at its specific implementation logic:

Implementation method

We explained how the library was used, so how did we do it? Let's take a look at the source code of the class library.

    • Prevent buttons from repeating clicks
/** * Method button repeated click on the source of the Listening class * * Public Abstract  class onclickfastlistener extends baseclicklistener {    //Prevent Quick click on default wait length of 900ms    Private LongDelay_time = the;Private Static LongLastclicktime;Private Boolean Isfastdoubleclick() {LongTime = System.currenttimemillis ();LongTimeD = Time-lastclicktime;if(0< TimeD && TimeD < Delay_time) {return true; } lastclicktime = time;return false; }@Override     Public void OnClick(View v) {//Determine if the current click event is less than the threshold of the previous click event time interval        if(Isfastdoubleclick ()) {return;    } onfastclick (v); }/** * Set default Quick Click event time interval * @param delay_time * @return * *     PublicOnclickfastlistenerSetlastclicktime(LongDelay_time) { This. Delay_time = Delay_time;return  This; }/** * Quick Click event callback method * @param v */     Public Abstract void Onfastclick(View v);}

The above is the onfastclicklistener that we prevent the button repeatedly click the source code, you can see here we define a Onclicklistener object to prevent repeated clicks, and rewrite its OnClick method, You can see that we call the Isfastdoubleclick method in the OnClick method, which is the implementation of the repeated click Logic, The Isfastdoubleclick method returns True when the time between the last click of the button and the time of the click is less than 900ms, when the OnClick method returns directly and does not perform the subsequent Onfastclick method, Otherwise, the Onfastclick method is executed directly. Then we just need to rewrite the Onfastclick method when we set the Click event for our view component ...

    • button tap to monitor network status
/** * Determine whether the current device has network monitoring class source * * Public Abstract  class onclicknetworklistener extends baseclicklistener {    @Override     Public void OnClick(View v) {BooleanIsnetworkok = isnetworkconnected (V.getcontext ());if(Isnetworkok)        {Onnetworkclick (v); }Else{Onnonetworkclick (v); }    }//Click Events--Have network     Public Abstract void Onnetworkclick(View v);//Click Events-No network     Public Abstract void Onnonetworkclick(View v);/** * Network connection is normal * * @param context * @return  * *     Public Static Boolean isnetworkconnected(Context context) {if(Context! =NULL{Connectivitymanager Mconnectivitymanager = (connectivitymanager) context. Getsystemservi            CE (context.connectivity_service); Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();if(Mnetworkinfo! =NULL) {returnMnetworkinfo.isavailable (); }        }return false; }}

Similar to the Just Prevent button repeat click event, here is also a rewrite of its own Onclicklistener object, and then rewrite its OnClick method, and in which to execute the Isnetworkconnected method, the method is to determine whether the current device has a network, If there is a network to execute the Onnetworkclick method, if there is no network to execute the Onnonetworkclick method, So when we set the Click event for our view component, we only need to set Onclicklistener for our custom Onclicklistener object for the view component. Then rewrite the Onnetworkclick and Onnonetworkclick methods, where the Onnetworkclick method is a callback method with a network, and the Onnonetworkclick method is a non-network callback method.

    • button Click to listen for login
/** * To determine whether the current app user is logged in the source of listening * * Public Abstract  class onclickloginedlistener extends baseclicklistener {    PrivateActivity context =NULL; Public Onclickloginedlistener(Activity context) { This. Context = Context; }@Override     Public void OnClick(View view) {Super. OnClick (view);if(islogined (context, view))        {Onloginedclick (view); }Else{Onnologinedclick (view); }    }/** * Determine if the current user is logged in * @param context * @param View * @return  */     Public Abstract Boolean islogined(Activity context, view view);/** * Logic to execute after user login * @param v */     Public Abstract void Onloginedclick(View v);/** * User not logged in to perform click events * /     Public Abstract void Onnologinedclick(View v);}

This is also done by defining its own Onclicklistener class, then overriding the OnClick method in it, and executing the islogined method in it, which is used to return the logical judgment of whether the user is logged in, and is also an abstract method, So we also need to implement its specific logic in the business layer, and then we rewrite the Onloginedclick method and the Onnologinedclick method, where the Onloginedclick method is the callback method after the user logs in, The Onnologinedclick method is the callback method that is executed after the user is not logged on.

    • Custom execution of the appropriate business logic
/** * Implementation of customized judgment logic of the Listening class source * * Public Abstract  class onclickcostomlistener extends baseclicklistener {    @Override     Public void OnClick(View view) {Super. OnClick (view);if(Iscorrect ())        {Oncorrentclick (view); }Else{Onnocorrentclick (view); }    }/** * Determine if logic passes * @return  */     Public Abstract Boolean Iscorrect();/** * Logical request after correct judgment * @param v */     Public Abstract void Oncorrentclick(View v);/** * Logical request executed after judgment failure * @param v */     Public Abstract void Onnocorrentclick(View v);}

You can see that a Onclicklistener class is redefined here, then the OnClick method is overridden, the method Iscorrect is executed first, and then the Oncorrentclick method is executed if it is judged. The Onnocorrentclick method is executed if the judgment is not passed.

In this way, we will probably analyze the main implementation logic and function of preventing button repeated click Class Library, source code is very simple, I will continue to open source and update some good class library, I hope that we support a lot.

Summarize:

    • The class library is mainly implemented by customizing the Onclicklistener class and overriding the OnClick method;

    • By setting the callback method to the abstract method, we must rewrite the corresponding callback method;

    • Project Save Address: Android-repeatclick, Welcome to star and follow


In addition to the GitHub project, open source project analysis of interested students can refer to my:
GitHub Project Resolution (i) –> upload Android project to GitHub
GitHub Project Resolution (ii) –> to publish Android projects to the Jcenter code base
GitHub Project Resolution (III) –>android memory leak monitoring leakcanary
GitHub Project Resolution (iv) –> dynamically change the font size of TextView
GitHub Project Parsing (v) –>android log Framework
GitHub Project Resolution (VI) –> custom implementation Butterknife Framework

GitHub Project Parsing (vii)-Prevent button repeat click

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.