Android 滑块验证

先上图看看实现效果

Android 滑块验证_第1张图片

 

1.在 app 的 build.gradle 添加依赖

implementation 'com.luozm.captcha:captcha:1.1.2'

2.将 Captcha 添加至布局文件

 

3.添加 Java 代码

    public void showw(String advPic) {

        Context context = MainActivity.this;
        diaa = new Dialog(context, R.style.edit_AlertDialog_style);
        diaa.setContentView(R.layout.item_pict);

        captcha = diaa.findViewById(R.id.captCha);
        final Button btn_mode = diaa.findViewById(R.id.btn_mode);
        final Captcha captcha = diaa.findViewById(R.id.captCha);
        ImageView cuowu = diaa.findViewById(R.id.im_cuowu);
        // Glide.with(getApplication()).load(advPic).into(captcha );
        captcha.setBitmap(advPic);

        btn_mode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (captcha.getMode() == Captcha.MODE_BAR) {
                    captcha.setMode(Captcha.MODE_NONBAR);
                    btn_mode.setText("滑动条模式");
                } else {
                    captcha.setMode(Captcha.MODE_BAR);
                    btn_mode.setText("无滑动条模式");
                }
            }
        });

        cuowu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                diaa.dismiss();
                itData(mLongitude, mLatitude);
            }
        });

        captcha.setCaptchaListener(new Captcha.CaptchaListener() {
            @Override
            public String onAccess(long time) {
                Toast.makeText(MainActivity.this, "验证成功", Toast.LENGTH_SHORT).show();


                diaa.dismiss();
                diaa.setCanceledOnTouchOutside(true);
                return "验证通过";

            }

            @Override
            public String onFailed(int count) {
                Toast.makeText(MainActivity.this, "验证失败,失败次数" + count, Toast.LENGTH_SHORT).show();
                return "验证失败";
            }

            @Override
            public String onMaxFailed() {
                Toast.makeText(MainActivity.this, "验证超过次数,你的帐号被封锁", Toast.LENGTH_SHORT).show();
                return "可以走了";
            }

        });

        diaa.show();

        diaa.setCanceledOnTouchOutside(true);


        Window w = diaa.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        lp.y = 40;
        diaa.onWindowAttributesChanged(lp);
 
    }

 

 

转载于:https://www.cnblogs.com/monkey0928/p/9864094.html

你可能感兴趣的:(Android 滑块验证)