1. 程式人生 > >自定義的可拖動滑塊驗證碼的實現方式(SlideView)

自定義的可拖動滑塊驗證碼的實現方式(SlideView)

概述:
最近專案中需要在密碼輸入一定次數後盡心拖動滑塊進行驗證,防止惡意程式程式碼一直測試登入,用了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檔案中就可以設定,官方文件就有說明,至此就完成了滑塊驗證的自定義功能