随机生成验证码

来源:互联网 发布:网络销售保健品诈骗案 编辑:程序博客网 时间:2024/06/06 11:41

  

  

 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,防止背景跟字体分不清

0 0
原创粉丝点击