Simple implementation of Android verification code

Simple implementation of Android verification code

We often at login or registration required to enter a verification code, here briefly a way to
effect the following

Write picture description here


The first is to get a random combination of four letters. Here, I store 26 letters in an array, and then randomly generate 4 subscript values, and take the letters corresponding to these four subscript values ​​as the verification code.

public class RandomChars {
    char[] chars;

    public RandomChars() {
        chars = new char[26];
        for (int i = 0; i < 26; i++) {
            chars[i] = (char) (i + 65);
        }
    }

    public char[] get4Chars() {
        char[] rlt = new char[4];
        for (int i = 0; i < rlt.length; i++) {
            int randomIndex = (int) (Math.random() * 26);
            rlt[i] = chars[randomIndex];
        }
        return rlt;
    }
}

Customize a CodeView to draw the verification code, which is mainly operated in the onDraw method. The learning skills are not good, and the size and position cannot be controlled in onMeasure.

        float unitWidth = (float) getWidth() / (float) chars.length;
        for (int i = 0; i < chars.length; i++) {
            String str = chars[i] + "";
            textPaint.getTextBounds(str, 0, str.length(), mRect);
            resetColor();
            int angel = (int) (Math.random()*(8-(-8)+1)+(-8));
            canvas.rotate(angel);//旋转字母,随机角度
            canvas.drawText(str, i * unitWidth + 5, getHeight() / 2 - mRect.centerY(), textPaint);
            /**
             * 很关键,旋转
             */
            canvas.save();//保存状态
            canvas.restore();//恢复
        }

/**
 * 重新设置随机颜色
 */
    private void resetColor() {
        int r = (int) (Math.random() * 230 - 30);
        int g = (int) (Math.random() * 230 - 30);
        int b = (int) (Math.random() * 230 - 30);
        textPaint.setColor(Color.rgb(r, g, b));
    }

Set the control and pass in four characters and it is ok. When verifying whether the input is correct, consider the case of capitalization, so convert all the input letters to uppercase, generally not case sensitive.

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputStr = input.getText().toString();
                inputStr = inputStr.toUpperCase();
                str = str.toUpperCase();
                if (str.equals(inputStr)) {
                    Toast.makeText(MainActivity.this, "输入正确", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "验证码输入错误", Toast.LENGTH_SHORT).show();
                    char[] getchar = randomChars.get4Chars();
                    str = new String(getchar);
                    codeView.setChars(getchar);
                }
            }
        });

I feel that there are still many shortcomings, continue to improve in the future!

Guess you like

Origin blog.csdn.net/lizebin_bin/article/details/51035050