RxAndroid防止按鈕短時間內被重複點選多次
阿新 • • 發佈:2019-01-24
- 匯入依賴
compile ‘io.reactivex:rxandroid:1.2.1’
compile ‘io.reactivex:rxjava:1.1.6’
- 新增許可權
//獲得寫sd卡的許可權
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
//獲得讀sd卡的許可權
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">< /uses-permission>
//讀取網路的許可權
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
- 建立一個class類:MyOnSubscribe.java
public class MyOnSubscribe implements Observable.OnSubscribe<View> {
public MyOnSubscribe(View view) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("我被狂點中...");
//釋出通知
mSubscriber.onNext(view);
}
});
}
private Subscriber mSubscriber;
@Override
public void call(Subscriber<? super View> subscriber) {
mSubscriber = subscriber;
}
}
- 在要寫點選事件的類中將點選事件換成下面的方法(這裡是button)
private void RxAndroidClick() {
//mButton是按鈕的物件
Observable.create(new MyOnSubscribe(mButton))
.throttleFirst(1, TimeUnit.SECONDS)
.subscribe(new Action1<View>() {
@Override
public void call(View view) {
System.out.println("我在call方法中,我被點選了,有反應了");
}
});
}