概述:
最近项目中需要在密码输入一定次数后尽心拖动滑块进行验证,防止恶意程序代码一直测试登录,用了GitHub的一个开源库SlideView,但是直接依赖源码是改不了样式的,下边给出自定义的可拖动滑块验证的实现方式
效果图(其中圆角大小,背景色,文字样式都可以自定义):
1,添加依赖包
地址:https://github.com/MAXDeliveryNG/slideview
这是使用方法和效果的GitHub地址,但是我们使用的时候不要按照上边的依赖在线导入,这样是改不了源码的,只需要将源码下载下来,里边有个一module依赖到项目中就可以了
添加上述依赖后就可以直接使用了,不用再buildgradle中依赖
xml代码:
<ng.max.slideview.SlideView
android:id="@+id/slideView"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@drawable/losepwdimgcode_kuang"
android:layout_marginLeft="@dimen/margin_20dp"
android:layout_marginRight="@dimen/margin_20dp"
android:layout_marginTop="10dp"
app:buttonBackgroundColor="@color/colorAccent"
app:buttonImage="@drawable/ic_chevron_double_right_white_24dp"
app:slideBackgroundColor="@android:color/white"
app:slideText="Accept"
app:slideTextColor="@color/colorAccent"
app:strokeColor="@color/colorAccent" />
java代码中设置滑动到头的监听就完事了
SlideView slideView = (SlideView) findViewById(R.id.slider);
slideView.setOnSlideCompleteListener(new SlideView.OnSlideCompleteListener() {
@Override
public void onSlideComplete(SlideView slideView) {
// vibrate the device
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(100);
// go to a new activity
startActivity(new Intent(MainActivity.this, NewActivity.class));
}
});
附:滑块的自定义方式:
属性:
其他属性的自定义(只允许滑动一次,从右向左滑动等)在xml文件中就可以设置,官方文档就有说明,至此就完成了滑块验证的自定义功能