Simple implementation method of Android local verification code (preventing violent login)

0. Preface

The verification code is everywhere. Someone asked me, do you know what is under the Da Vinci password? Yes, the answer is the Da Vinci verification code.

One of the most important functions of the verification code is to prevent malicious brute force login and uninterrupted login attempts. Some people say that it can actually detect the login interval of the terminal on the server. If the interval is too short, it can show the attitude of rejection. However, the role of local verification code is more practical, which can reduce the pressure on the server. This article will use a custom view to implement a simple local verification code with the following effects. It's a review of custom view knowledge.

1. Layout structure

The custom attribute is used in the custom control MyView, which is occasionally asked during the interview. In fact, it is not difficult. There are three custom attributes: text content, color and font size. Don't forget to add the namespace.

The custom attribute declaration only needs to declare an XML file in the values directory. The file name is not important, but the name attribute is important, because we will find the custom attribute declaration information through r.styleable.myview in the custom control class.

2. Custom view class

Take a look at the constructor of this class:

The core code is to parse the user-defined attributes, initialize a brush, set the parsed font size to the brush, design the click time, randomly generate the four digit verification code after it is clicked, and refresh the interface with postinvalidate(). Finally, use mbound to record the width and height of the four digit text.

2. Other details in the custom view class

In fact, it is mainly the process of measure and draw.

The most important logic in the onmeasure () method should be to handle measurespec. At_ In most, the previous mbound. Width () works. In addition, no matter what measurement mode, padding is handled manually.

In the OnDraw () method, first draw a yellow rectangle as the background of the custom view, then draw four digits according to the text content and color in the custom attribute, and finally draw four noise lines with random color, and the start position and end position are also generated randomly.

3. Change the correct verification code of maintenance in real time

In order to verify the correctness of the verification code entered by the user, a variable needs to be maintained in mainactivity. When the user clicks custom view to refresh the verification code, the value of this variable can be changed in real time. Here, a user-defined broadcast is used to generate a random four digit number and send a user-defined broadcast.

Then register a broadcast receiver in mainactivity to obtain the verification code information at this time. After the user clicks the OK button, he can get the value in EditText and compare it with it. This logic is relatively simple.

The above is the simple implementation method of Android local verification code (preventing violent login) introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>