Android 滑块验证
先上图看看实现效果
1.在 app 的 build.gradle 添加依赖
implementation 'com.luozm.captcha:captcha:1.1.2'
2.将 Captcha 添加至布局文件
<com.luozm.captcha.Captcha android:id="@+id/captCha" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="265dp" android:layout_marginRight="10dp" android:layout_marginLeft="10dp" app:mode="mode_bar" app:src="@drawable/validate" app:blockSize="50dp" android:scaleType="centerCrop" app:max_fail_count="4"/>
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); }
努力不是表现给谁看的,而是从小白渐渐走向牛X的过程!!!