随机生成验证码

  

  

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值