Activity: @Overrideprotected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); linearlayout linearlayout = New linearlayout (this); verifycodeview vc = new verifycodeview (this,6); textview textview = new textview (this); textview.settext ("Verification Code:"); edittext mincode = new edittext ( this); vc.setonclicklistener (New view.onclicklistener () { @Override public void OnClick (VIEW&NBSP;V) { V.postinvalidate ()//re-call Verifycodeview construction method, update Captcha toast.maketext (GetaPplicationcontext (), "Updat code", Toast.length_long). Show (); } linearlayout.addview (TextView); linearlayout.addview (Mincode); linearlayout.addview (VC); Setcontentview (linearlayout);} view:private character[] codes = {' Q ', ' W ', ' e ', ' r ', ' t ', ' y ', ' u ', ' I ' , ' O ', ' P ', ' a ', ' s ', ' d ', ' f ', ' g ', ' H ' , ' J ', ' K ', ' l ', ' Z ', ' x ', ' C ', ' V ', ' B ' , ' n ', ' m ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ' , ' 7 ', ' 8 ', ' 9 ', ' 0 '};p rivate character[] vcode;public verifycodeview (Context context) { Super (context); vcode = new character[4];//defines the length of the verification code}public Verifycodeview (context context, int codesize) { super (Context); vcode = new character[codesize];//defines the length of the Verification code} @Overrideprotected void ondraw (Canvas canvas) { super.ondraw (Canvas); Vcode = getcodes (codes); paint pen = new paint (); pen.setcolor (COlor. white); int mwidth = getwidth (); canvas.drawrect ( Mwidth*2/3,10.0f,mwidth-20,100.0f,pen); random r = new random (100); int padding = 1; for (int i = 0; i < vcode.length; i++) { Pen.setcolor (Color.rgb (R.nextint (), R.nextint (), R.nextint ())); Pen.settextsize, //canvas.drawtext (new char[]{' A ', ' C ', ' B ', ' d ', ' 1 ', ' 2 ', ' e '},0,7,mwidth*2/3+10,70.0f,pen); //string text = "H" +r.nextint () + "E" +r.nextint () + "L"; Canvas.drawtext (Vcode[i].tostring (), Mwidth*2/3+padding,70.0f,pen); padding = padding + 35; }}public character[] getcodes (Character[] Codes) { random rd = new random (); for ( int i = 0; i < vcode.length; i++) { vcode[i] = codes[rd.nextint (]; } ) Return vcode;}
Android Verification Code (canvas)