Android development verification code generation, android development verification code

Source: Internet
Author: User

Android development verification code generation, android development verification code

Recently, I am working on e-commerce finance projects. The verification code generation method is essential. I first learned a kind of code that has been tested for ease of use and learned from other places, after a slight modification, you can choose whether to support case-sensitive identification. Directly add the code.

Import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. editText; import android. widget. imageView; import android. widget. toast;
Public class VerifyCodeActivity extends Activity {
Private ImageView vc_image; // Verification Code icon private String getCode = null; // obtain the verification code value private EditText vc_code; // The value of the text box
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. verifycode); vc_image = (ImageView) findViewById (R. id. verify_imv); vc_code = (EditText) findViewById (R. id. myedit); getCode = SecurityCode. getInstance (). getCode (false); // obtain the displayed Verification Code findViewById (R. id. vc_shuaxin ). setOnClickListener (new OnClickListener (){
@ Override public void onClick (View v) {vc_image.setImageBitmap (SecurityCode. getInstance (). getBitmap (); getCode = SecurityCode. getInstance (). getCode (false) ;}}); findViewById (R. id. verfiy ). setOnClickListener (new OnClickListener (){
@ Override public void onClick (View v) {String verfiyString = vc_code.getText (). toString (); if (verfiyString. equals (getCode) {Toast. makeText (getApplicationContext (), "The verification code is entered correctly", Toast. LENGTH_LONG ). show ();} else {Toast. makeText (getApplicationContext (), "Incorrect verification code input", Toast. LENGTH_LONG ). show ();}}});}}
Import java. util. Locale; import java. util. Random;
Import android. graphics. Bitmap; import android. graphics. Bitmap. Config; import android. graphics. Canvas; import android. graphics. Color; import android. graphics. Paint;
/*** @ Description: verification code generation class */public class SecurityCode {
Private static final char [] CHARS = {'0', '1', '2', '3', '4', '5', '6 ', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G ', 'H', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q ', 'R', 's', 't', 'U', 'V', 'w', 'x', 'y', 'z', 'A ', 'B', 'C', 'D', 'E', 'F', 'G', 'h', 'I', 'J', 'k ', 'l', 'M', 'n', 'O', 'P', 'Q', 'R', 's', 't', 'U ', 'V', 'w', 'x', 'y', 'z '};
Private static SecurityCode bpUtil;
Public static SecurityCode getInstance () {if (bpUtil = null) bpUtil = new SecurityCode (); return bpUtil ;}
// Default settings private static final int DEFAULT_CODE_LENGTH = 4; // The verification code length is 4 private static final int DEFAULT_FONT_SIZE = 60; // font size: private static final int DEFAULT_LINE_NUMBER = 3; // Number of interfering lines: private static final int BASE_PADDING_LEFT = 20; // left margin: private static final int RANGE_PADDING_LEFT = 35; // The left margin value is private static final int BASE_PADDING_TOP = 42; // The top margin is private static final int RANGE_PADDING_TOP = 15; // The top margin value is private static final int DEFAULT_WIDTH = 200; // default width. the total width of the image is private static final int DEFAULT_HEIGHT = 70; // The default height. the total height of the image is private final int DEFAULT_COLOR = 0xdf; // Default background color value
// Settings decided by the layout xml // canvas width and height private int width = DEFAULT_WIDTH; private int height = DEFAULT_HEIGHT;
// Random word space and pading_top private int base_padding_left = BASE_PADDING_LEFT; private int range_padding_left = RANGE_PADDING_LEFT; private int placement = inline; private int range_padding_top = inline;
// Number of chars, lines; font size private int codeLength = DEFAULT_CODE_LENGTH; private int line_number = DEFAULT_LINE_NUMBER; private int font_size = DEFAULT_FONT_SIZE;
// Variables private String code; // Save the generated Verification code private int padding_left, padding_top; private Random random = new Random ();
Private Bitmap createBitmap () {padding_left = 0;
Bitmap bp = Bitmap. createBitmap (width, height, Config. ARGB_8888); Canvas c = new Canvas (bp );
Code = createCode ();
C. drawColor (Color. rgb (DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR); Paint paint = new Paint (); paint. setTextSize (font_size );
For (int I = 0; I <code. length (); I ++) {randomTextStyle (paint); randomPadding (); c. drawText (code. charAt (I) + "", padding_left, padding_top, paint );}
For (int I = 0; I <line_number; I ++) {drawLine (c, paint );}
C. save (Canvas. ALL_SAVE_FLAG); // save c. restore (); // return bp ;}
/*** @ Description: Case Sensitive */public String getCode (boolean isMatchCase) {if (code! = Null) {code. toLowerCase (Locale. getDefault () ;}} else {return code ;}
Return null ;}
Public Bitmap getBitmap () {return createBitmap ();}
Private String createCode () {StringBuilder buffer = new StringBuilder (); for (int I = 0; I <codeLength; I ++) {buffer. append (CHARS [random. nextInt (CHARS. length)]);} return buffer. toString ();}
Private void drawLine (Canvas canvas, Paint paint) {int color = randomColor (); int startX = random. nextInt (width); int startY = random. nextInt (height); int stopX = random. nextInt (width); int stopY = random. nextInt (height); paint. setStrokeWidth (1); paint. setColor (color); canvas. drawLine (startX, startY, stopX, stopY, paint );}
Private int randomColor () {return randomColor (1 );}
Private int randomColor (int rate) {int red = random. nextInt (256)/rate; int green = random. nextInt (256)/rate; int blue = random. nextInt (256)/rate; return Color. rgb (red, green, blue );}
Private void randomTextStyle (Paint paint) {int color = randomColor (); paint. setColor (color); paint. setFakeBoldText (random. nextBoolean (); // true indicates bold, and false indicates non-bold float skewX = random. nextInt (11)/10; skewX = random. nextBoolean ()? SkewX:-skewX; paint. setTextSkewX (skewX); // float type parameter. A negative number indicates the Right oblique, and an integer indicates the left oblique. // paint. setUnderlineText (true); // true indicates the underline, and false indicates the non-Underline. // paint. setStrikeThruText (true); // true indicates strikethrough, and false indicates non-strikethrough}
Private void randomPadding () {padding_left + = base_padding_left + random. nextInt (range_padding_left); padding_top = random + random. nextInt (range_padding_top );}}
Layout file <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical"> <ImageView
Android: id = "@ + id/verify_imv"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/> <Button
Android: id = "@ + id/vc_shuaxin"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "get verification code"/> <EditText
Android: id = "@ + id/myedit"
Android: layout_width = "200dp"
Android: layout_height = "wrap_content"
Android: hint = "Enter the verification code"/> <Button
Android: id = "@ + id/verfiy"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "verify that the entered verification code is correct"/> </LinearLayout>




Android Developers get mobile phone verification Codes

Work with the carrier and they call the interface for you.

How to dynamically generate multiple interfaces in android Development

I think we can.
1. parse the questions in excel to generate a set of question libraries. Of course, you must reserve the attributes you want to record for each question, including the results and types...
2. layout classification. For a single choice, you can write a single choice layout, multiple choice, multiple choice layout, and Q & A write a Q & A layout. These three la s are displayed based on the type of each question.
3. The previous question and next question are associated with the question library set, representing the location of the question library set, so that you can determine which type of layout the next question should display based on the question type.
In this way, the display mode can be controlled using only one interface and multiple sub-la S.

Related Article

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.