activity:@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);LinearLayoutlinearLayout=newLinearLayout(this);VerifyCodeViewvc=newVerifyCodeView(this,6);TextViewtextView=newTextView(this);textView.setText("验证码:");EditTextmInCode=newEditText(this);vc.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){v.postInvalidate();//重新调用VerifyCodeView的构造方法,更新验证码Toast.makeText(getApplicationContext(),"updatcode",Toast.LENGTH_LONG).show();}});linearLayout.addView(textView);linearLayout.addView(mInCode);linearLayout.addView(vc);setContentView(linearLayout);}view:privateCharacter[]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'};privateCharacter[]vcode;publicVerifyCodeView(Contextcontext){super(context);vcode=newCharacter[4];//定义验证码的长度}publicVerifyCodeView(Contextcontext,intcodeSize){super(context);vcode=newCharacter[codeSize];//定义验证码的长度}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);vcode=getCodes(codes);Paintpen=newPaint();pen.setColor(Color.WHITE);intmwidth=getWidth();canvas.drawRect(mwidth*2/3,10.0f,mwidth-20,100.0f,pen);Randomr=newRandom(100);intpadding=1;for(inti=0;i<vcode.length;i++){pen.setColor(Color.rgb(r.nextInt(),r.nextInt(),r.nextInt()));pen.setTextSize(60);//canvas.drawText(newchar[]{'a','c','b','d','1','2','e'},0,7,mwidth*2/3+10,70.0f,pen);//Stringtext="h"+r.nextInt()+"e"+r.nextInt()+"l";canvas.drawText(vcode[i].toString(),mwidth*2/3+padding,70.0f,pen);padding=padding+35;}}publicCharacter[]getCodes(Character[]codes){Randomrd=newRandom();for(inti=0;i<vcode.length;i++){vcode[i]=codes[rd.nextInt(36)];}returnvcode;}

更多相关文章

  1. android创建自定义对话框
  2. Android 自定义系统菜单的背景源码
  3. Android视频播放项目总结之 使用Android中的videoView自己定义,暂
  4. Android 自定义滚动视图
  5. Android 自定义文本框(带图片)
  6. Android 自定义View之能力分析图
  7. android中自定义的一个可以弹出类似于提示更新的对话框
  8. [Android]自定义系统菜单的背景
  9. android editText 自定义边框

随机推荐

  1. Android(安卓)开发艺术探索笔记之七 -- A
  2. 【Android您问我讲】Android(安卓)2.x中
  3. android学习日记01-搭配开发环境
  4. Android进程 Handler Message Looper
  5. 移动互联网的新宠:Android之缤纷世界
  6. 处女男学Android(十五)---Android(安卓)广
  7. 第1章 android发展史
  8. Android音频开发(1):基础知识
  9. Android撬动IT市场的新支点
  10. Android有用代码片断(六)