Android prevent button from clicking repeatedly example code

In this article, I will introduce a small tool class library encapsulated by myself: the button click event class library.

Function: this class library can prevent repeated clicking of buttons, judge network status, user login status, user-defined authentication conditions, etc.

Note: the core principle of its implementation is to implement its own onclicklistener class through customization, rewrite its onclick method, and call back our customized abstract method after executing the corresponding judgment logic in the onclick method.

The specific effect is shown in the figure below:

Mode of use

Block multiple click events

The fastbutton here is an ordinary button component, and then we set the click event for this component, and the onclicklistener class we defined is passed in (it should be noted here that the click event of any view component is OK, not just the button component, but only the button component as an example), The default time interval for shielding multiple click events here is 900ms, that is, after we set our customized click event listening for the component, if there are two click events, and if the second click is less than 0.9s from the first click event, the second click will not work;

Shielding network conditions

Similarly, the networkbutton here is also a button component customized by us. We set click event listening for it and passed in our customized onnetworkclicklistener class. It can be found that there are two callback methods in onnetworkclicklistener class. Among them, onnetworkclick method is used to execute the situation that the current device has a network, The onnonetworkclick method is used to handle subsequent operations when the current device has no network;

Whether to screen login

The loginButton here is also a custom button component, and our onloginedclicklistener class is set for it, and then there are three callback methods,

The method islogined is used to determine whether the current user is logged in. If it returns true, it means that the user has logged in. If it returns false, it means that the user has not logged in. The specific implementation logic needs to be implemented in the business layer. The method onloginedclick is used to execute the logic after login, and the method onnologinedclick is used to execute the logic that the user has not logged in.

This is the general implementation function of this class library. After that, let's take a look at its specific implementation logic:

Implementation mode

We explained how to use the class library above, so how do we implement it? Let's take a look at the source code of the class library.

Prevent repeated button clicks

The above is the source code of OnFastClickListener for preventing repeated click of the button. We can see that we define the OnClickListener object to prevent repeated clicks and rewrite the onClick method. We can see that we call the isFastDoubleClick method in the onClick method, which is whether we repeatedly click the logic. When the time interval between the last click of the button and the current click is less than 900ms, the isfastdoubleclick method will return to true. At this time, the onclick method will return directly and will not execute the subsequent onfastlick method. Otherwise, the onfastlick method will be executed directly. Then, when setting the click event for our view component, we just need to override the onfastlick method...

Click the button to monitor the network status

Similar to the event of preventing repeated clicking of the button just now, we also rewrite our onclicklistener object, then rewrite its onclick method, and execute the isnetworkconnected method in it. This method is to judge whether the current device has a network. If there is a network, execute the onnetworkclick method. If there is no network, execute the onnonetworkclick method, In this way, when setting click events for our view component, we only need to set onclicklistener for the view component as our customized onclicklistener object, and then rewrite the onnetworkclick and onnonetworkclick methods, where onnetworkclick method is a callback method with network and onnonetworkclick method is a callback method without network.

Click the button to listen for login

Here, we also define our own onclicklistener class, rewrite the onclick method, and execute the islogined method. This method is used to return the logical judgment of whether the user logs in. It is also an abstract method, so we also need to implement its specific logic in the business layer, Then we rewrite the onloginedclick method and the onnologinedclick method, where the onloginedclick method is the callback method after the user logs in, and the onnologinedclick method is the callback method executed after the user does not log in.

Customized execution of corresponding business logic

You can see that an onclicklistener class is redefined here, and then the onclick method is rewritten. First, the judgment method iscorrect is executed, then the oncorrentlick method is executed if the judgment passes, and the onnocorrentlick method is executed if the judgment fails.

In this way, we have roughly analyzed the main implementation logic and functions of the class library to prevent repeated click of buttons. The source code is very simple. In the future, I will continue to open source and update some easy-to-use class libraries. I hope you can give me more support.

Summary:

The class library is mainly implemented by customizing the onclicklistener class and overriding the onclick method;

Setting the callback method as an abstract method ensures that we must rewrite the corresponding callback method;

Project storage address: Android repeatlick. Welcome to star and follow

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>