您的位置:首页 > 其它

随机生成验证码

2015-09-19 18:03 162 查看
  

   private static int r;
private static int g;
private static int b;

private static StringBuffer buffer;
public static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
public static Random random = new Random();

// 获取8位随机数
public static String getRandomString() {
// 缓存字符串
buffer = new StringBuffer();
for (int i = 0; i < 6; i++) {
// 每次取一个随机字符
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}
public static void color(){
getRandomString();
r=(int) (Math.random()*255);
g=(int) (Math.random()*255);
b=(int) (Math.random()*255);
denglu_textview1.setText(getRandomString());
denglu_textview1.setTextColor(Color.rgb(r, g, b));
denglu_textview1.setBackgroundColor(Color.rgb(255-r,255-g, 255-b));

}

Xml布局,使用TextView  

从26个字母和10个数字中随机取出6个字符, ,除去"l","1""0"0'等相似的字符,

颜色使用rgb

setText(Color.rgb(r,g,b);

利用Math.random()*255随机生成r,g,b范围是0-255

背景使用反色:255-r,255-g,255-b,防止背景跟字体分不清
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  验证码 随机数 String