Android uses custom view to implement random authentication code _android

Source: Internet
Author: User

A lot of Android Getting Started program apes may be more fearful about Android custom view, but this is the only way to get to the master's level, all ready to spend some time on custom view and write more articles.

I. Description of the problem

Familiar with the web development of children's shoes are known to prevent malicious cracking, malicious submission, brush tickets, etc. when we submit the form data, we will use the Random authentication code function. We also need this function in Android applications, how to do it, we will customize a random verification Code view control to achieve this requirement, and have the versatility, when needed in the interface directly add this view component.

Second, the case introduction

Case Run effect

Components involved in the case

1, Checkview custom code control, mainly rewrite the OnDraw method to achieve graphic drawing

2, config: For the validation code control parameters of the configuration, such as point points, the number of dashes, background color settings

3, Checkutil: Verification code related tools, such as random point coordinates, random line start and end point coordinates, verification code calibration functions

4. Mainactivity: Test Application

Third, the function realizes

1. Write Config component

/** * Function: Used for configuration of validation Code control parameters */public class Config {//Authenticode update time public static final int ptede_time = 1200;//DOT Set public st
atic final int point_num = 100;
Number of segments set public static final int line_num = 2;
Set background color public static final int color=color.blue;
Random data length public static int text_length=4;
Set Authenticode font size public static int text_size=30; 2, Checkutil component/** * Function: Verification Code related Tool class */public class Checkutil {/** * Generate random number * @return */public static int [] Getchecknum () {int [] Tempchecknum = new Int[config.text_length]; for (int i = 0; i < config.text_length; i++) {Tempchecknum[i] = (
int) (Math.random () * 10);
return tempchecknum; /** * The starting point coordinate and end point coordinate * of the randomly generated dash * @param height value of the incoming Checkview * @param width * @return The starting point coordinate and the end point coordinate/public Static int[] getline (int height, int width) {int [] Tempchecknum = {0,0,0,0}; for (int i = 0; i < 4; i+=2) {Tempchecknum
[I] = (int) (Math.random () * width);
Tempchecknum[i + 1] = (int) (Math.random () * height);
return tempchecknum; }/** * RandomThe center point coordinates of the generating point * @param height value of the incoming Checkview * @param width of the checkview value * @return/public static int[] GetPoint (int hei  ght, int width) {int [] Tempchecknum = {0,0,0,0}; tempchecknum[0] = (int) (Math.random () * width); tempchecknum[1] = (int)
(Math.random () * height);
return tempchecknum;  /** * Verify that the correct * @param usercheck user Input Verification code * @param the random number generated by the validation control * @return/public static Boolean Checknum (String
Usercheck, int[] checknum) {if (Usercheck.length ()!= 4) {return false;}
String checkstring = ""; for (int i = 0; i < 4; i++) {checkstring + = Checknum[i]; if (Usercheck.equals (checkstring)) {return true;} else {re
Turn false; }/** * Compute the plot y-point position of the captcha * @param height value of the incoming Checkview * @return/public static int Getpositon (int height) {int Tempposi
Toin = (int) (Math.random () * height);
if (Temppositoin <) {Temppositoin +} return temppositoin; }
}

3, custom validation code control Checkview

The public class Checkview extends view{the context mcontext; int [] checknum = null;
Paint mtemppaint = new Paint (); Authentication Code Public Checkview (context, AttributeSet attrs) {Super (context, attrs); mcontext = context; Mtemppaint.setan
Tialias (TRUE);
Mtemppaint.settextsize (config.text_size);
Mtemppaint.setstrokewidth (3); public void OnDraw (Canvas Canvas) {canvas.drawcolor (config.color); final int height = getheight ();//Get height of Checkview control fi
nal int width = getwidth ();//Get the width of the checkview control int dx = 40; for (int i = 0; i < 4 i + +) {//Draw the text on the validation control Canvas.drawtext ("" + checknum[i], DX, Checkutil.getpositon (height), mtemppaint
);
DX + + WIDTH/5;
int [] line; for (int i = 0; i < config.line_num i + +) {//Dash line = checkutil.getline (height, width); Canvas.drawline (line[0), line[1
], line[2], line[3], mtemppaint);
//Draw dot int [] point; for (int i = 0; i < config.point_num i + +) {//Draw point Point=checkutil.getpoint (height, width); canvas.drawcircle (Point[0], p
Oint[1], 1, mtemppaint); }} public void Setchecknum (int [] chencknum) {//Set authentication code checknum = Chencknum;} public int[] Getchecknum () {//Get authentication code return checknum; pub LIC void Invalichenknum () {invalidate ();}}

4, writing mainactivity test code

public class Mainactivity extends activity implements view.onclicklistener{private checkaction mcheckview; private Text
View Mshowpassviwe;
Private EditText Meditpass;
Private Button Msubmit;
Private Button mref;
Verification code: private int [] Checknum =null; 
public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);
Initview ();
Initchecknum (); public void Initview () {Mcheckview = (Checkview) Findviewbyid (r.id.checkview); mshowpassviwe = (TextView) Findviewbyid (
R.id.checkpass);
Meditpass = (edittext) Findviewbyid (r.id.checktest);
Msubmit = (Button) Findviewbyid (r.id.submit);
Mref = (Button) Findviewbyid (R.ID.REF);
Msubmit.setonclicklistener (this);
Mref.setonclicklistener (this);
}//Initialize the authentication code and refresh the interface public void Initchecknum () {checknum = Checkutil.getchecknum (); Mcheckview.setchecknum (Checknum);
Mcheckview.invalichenknum (); public void OnClick (View v) {switch (V.getid ()) {case R.id.submit:string userinput = Meditpass.gettext (). ToSTring ();
if (Checkutil.checknum (Userinput, Checknum)) {setpassstring ("through");
Toast.maketext (This, "through", 1200). Show ();
}else{setpassstring ("failed");
Toast.maketext (This, "failed", 1200). Show ();
} break;
Case R.id.ref:initchecknum ();
Break
Default:break; } public void Setpassstring (String passstring) {mshowpassviwe.settext (passstring);}}

The above mentioned is for Android through the custom view to achieve random verification code related knowledge, hope to help!

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.