Android Random verification code-custom View and android Verification Code view
Familiar with web development, kids shoes know that they all use the Random verification code function when submitting form data to prevent malicious cracking, malicious submission, and ticket swiping. In Android applications, we also need this function. How can we implement this function? Next we will customize a random Verification Code View control to achieve this requirement, and it is universal, add this View component to the interface when necessary.
Case Running Effect
Components involved in the case
1. CheckView custom verification code control, which mainly overrides the onDraw Method for drawing
2. Config: used to configure the verification code control parameters, such as number of vertices, dashes, and background color.
3. CheckUtil: Verification Code tool, for example, random point coordinates, random line segment start and end point coordinates, verification code verification, etc.
4. MainActivity: test the application
| Iii. function implementation |
1. Compile the Config component
/*** Function: used to configure the parameters of the verification code control **/public class Config {// The Verification Code Update Time is public static final int PTEDE_TIME = 1200; // set public static final int POINT_NUM = 100; // set public static final int LINE_NUM = 2; // set the background COLOR to public static final int Color = COLOR. BLUE; // length of random data public static int TEXT_LENGTH = 4; // set the font size of the Verification Code public static int TEXT_SIZE = 30 ;}2. CheckUtil
Components
/*** Function: Verification Code tool **/public class CheckUtil {/*** generates a 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 ;} /*** generate the starting and ending points coordinates of the dashes randomly * @ param height input the CheckView height value * @ param width input the CheckView width value * @ return the start and end points coordinates */public static Int [] getLine (int height, int width) {int [] tempCheckNum = {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 ;} /*** randomly generate the coordinates of the center of the vertex * @ param height input the height value of the CheckView * @ param width input the width value of the CheckView * @ return */public static int [] getPoint (int height, int width) {int [] tempCheckNum = {0, 0, 0}; tempCheckNum [0] = (int) (Math. random () * width); tempCheckNum [1] = (int) (Math. random () * height); return tempCheckNum ;} /*** verify correctness * @ param userCheck the verification code entered by the user * @ param checkNum the random number generated by the 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 {return false ;}} /*** calculate the y point position of the Verification Code * @ param height the height of the CheckView passed in * @ return */public static int getPositon (int height) {int tempPositoin = (int) (Math. random () * height); if (tempPositoin <20) {tempPositoin + = 20;} return tempPositoin ;}}3. Custom verification code control CheckView
Public class CheckView extends View {Context mContext; int [] CheckNum = null; Paint mTempPaint = new Paint (); // verification code public CheckView (Context context, AttributeSet attrs) {super (context, attrs); mContext = context; mTempPaint. setAntiAlias (true); mTempPaint. setTextSize (Config. TEXT_SIZE); mTempPaint. setStrokeWidth (3);} public void onDraw (Canvas canvas) {canvas. drawColor (Config. COLOR); final int height = getHeight (); // obtain the height of the CheckView control final int width = getWidth (); // obtain the width of the CheckView control int dx = 40; for (int I = 0; I <4; I ++) {// draw the text canvas on the verification control. drawText ("" + CheckNum [I], dx, CheckUtil. getPositon (height), mTempPaint); dx + = width/5;} int [] line; for (int I = 0; I <Config. LINE_NUM; I ++) {// line = CheckUtil. getLine (height, width); canvas. drawLine (line [0], line [1], line [2], line [3], mTempPaint);} // draw a small dot int [] point; for (int I = 0; I <Config. POINT_NUM; I ++) {// point = CheckUtil. getPoint (height, width); canvas. drawCircle (point [0], point [1], 1, mTempPaint) ;}} public void setCheckNum (int [] chenckNum) {// set verification code CheckNum = chenckNum ;} public int [] getCheckNum () {// get the verification code return CheckNum;} public void invaliChenkNum () {invalidate ();}}4. Compile the MainActivity test code
Public class MainActivity extends Activity implements View. onClickListener {private CheckAction mCheckView; private TextView 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 verification 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 ("passed"); Toast. makeText (this, "passed", 1200 ). show ();} else {setPassString ("failed"); Toast. makeText (this, "failing", 1200 ). show ();} break; case R. id. ref: initCheckNum (); break; default: break;} public void setPassString (String passString) {mShowPassViwe. setText (passString );}}