Android Verification Code function Implementation code _android

Source: Internet
Author: User

First show you the effect of the picture, if you feel good, please refer to the implementation code

A very simple example, click on the Refresh Verification code, refresh the current display of the verification code, click OK, if the input and display match, it will jump to the next interface, here only to achieve the jump, and did not carry out other operations

Okay, here's the code.

First look at the layout of the Mainactivity

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context" =". Mainactivity "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content "Android:layout_" height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" Android:layout_
marginleft= "20DP" android:layout_margintop= "77DP" android:text= "Authenticode:"/> <edittext android:id= "@+id/vc_code" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignbaseline= "@+id/" TextView1 "android:layout_alignbottom=" @+id/textview1 "android:layout_torightof=" @+id/textview1 "android:ems=" "10"
> <requestfocus/> </EditText> <button android:id= "@+id/vc_ok" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignleft= "@+id/vc_image" android:layout_alignright= "@+i"D/vc_shuaixi "android:layout_below=" @+id/vc_shuaixi "android:layout_margintop=" 15DP "android:text=" OK "/> <
Button android:id= "@+id/vc_shuaixi" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_alignright= "@+id/vc_code" android:layout_aligntop= "@+id/vc_image" android:layout_marginRight= "
20DP "android:text=" Refresh authentication code "/> <imageview android:id=" @+id/vc_image "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_alignleft= "@+id/textview1" android:layout_below= "@+id/vc_" Code "android:layout_margintop=" 25DP "android:src=" "@drawable/ic_launcher"/> </RelativeLayout>

mainactivity Code

Package com.example.yanzhengma; Import Android.
r.string;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.content.ClipData.Item;
Import android.content.Intent;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.ImageView;
Import Android.widget.TextView;
Import Android.widget.Toast; public class Mainactivity extends activity implements Onclicklistener {ImageView vc_image;//Picture Button Vc_shuaixi, VC_OK
;
String getcode = null;
EditText Vc_code; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Vc_image = (ImageView) Findviewbyid (r.id.vc_image);
Vc_image.setimagebitmap (Code.getinstance (). Getbitmap ());
Vc_code = (edittext) Findviewbyid (R.id.vc_code); GetCode = Code.getinstance (). GetCode ();
Gets the displayed verification code Vc_shuaixi = (Button) Findviewbyid (R.id.vc_shuaixi); VC_shuaixi.setonclicklistener (this);
VC_OK = (Button) Findviewbyid (R.ID.VC_OK);
Vc_ok.setonclicklistener (this); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds the items to the action bar if it I
s present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true; @Override public void OnClick (view view) {//TODO auto-generated the stub switch (View.getid ()) {case R.id.vc_shuai
Xi:vc_image.setImageBitmap (Code.getinstance (). Getbitmap ());
GetCode = Code.getinstance (). GetCode ();
Break
Case R.id.vc_ok:string V_code = Vc_code.gettext (). toString (). Trim (); if (V_code = null | | v_code.equals ("")) {Toast.maketext (Mainactivity.this, "Authenticode is Empty", Toast.length_short). Show ();
else if (!v_code.equals (GetCode)) {Toast.maketext (mainactivity.this, "Authenticode error", Toast.length_short). Show ();
Toast.maketext (Mainactivity.this, "Validation successful", Toast.length_short). Show ();
Intent itintent = new Intent (mainactivity.this, Secondactivity.class); StartActivity (itintent);
MainActivity.this.finish ();
} break; }
}
}

OK, go to the key section, draw the verification code

Package com.example.yanzhengma;
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;  public class Code {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 Code Bputil;
Private Code () {}; public static Code getinstance () {if (Bputil = = null) Bputil = new Code (), return bputil;} private static Final int DEFA Ult_code_length = length of 4;//verification code here is the 4-bit private static final int default_font_size = 60;//font size private static final int Defau Lt_line_number = 3;//How many interference lines private static final int base_padding_left = 20; Left margin private static final int RANGE_padding_left = 35;//Left margin range value private static final int base_padding_top = 42;//Top margin private static final int range_padding_ top = 15;//upper margin range value private static final int default_width = 200;//default width. Picture's total width private static final int default_height = 70
//default height. Picture's total high private final int default_color = 0xdf;//default background color value private int width = default_width;
private int height = default_height;
private int base_padding_left = Base_padding_left;
private int range_padding_left = Range_padding_left;
private int base_padding_top = Base_padding_top;
private int range_padding_top = Range_padding_top;
private int codelength = Default_code_length;
private int line_number = Default_line_number;
private int font_size = Default_font_size;
Private String code;//Authentication 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) + "", Padd
Ing_left, Padding_top, paint);
for (int i = 0; i < Line_number i++) {drawLine (c, paint);} c.save (Canvas.all_save_flag);
C.restore ()//return to BP; public string GetCode () {return code;} public Bitmap Getbitmap () {return CreateBitmap ();} private String Createcode ( {StringBuilder buffer = new StringBuilder (); for (int i = 0; i < codelength; i++) {Buffer.append
NT (Chars.length)]);
return buffer.tostring (); } private void DrawLine (Canvas Canvas, Paint Paint) {int color = Randomcolor (); int startx = Random.nextint (width); int s
Tarty = 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 (ran Dom.nextboolean ());
True is bold and false is not bold float skewx = Random.nextint (11)/10; Skewx = Random.nextboolean ()?
SKEWX:-skewx; Paint.settextskewx (SKEWX); Float type parameter, negative number indicates right oblique, integer left oblique} private void randompadding () {padding_left + + Random.nextint + Range_paddin
G_left);
Padding_top = Base_padding_top + random.nextint (range_padding_top); }
}

All right, the code has a comment, it's supposed to look clear.

The above is a small set to introduce the Android verification code function code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.