rxjava实现倒计时秒数

本文介绍如何利用RxJava库在Android中创建一个倒计时功能,详细阐述了关键步骤和代码实现,帮助开发者理解如何将观察者模式应用于倒计时场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工具类

package com.ms.security.rx;

import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jakewharton.rxbinding3.widget.RxTextView;
import com.ms.security.bean.Bean;
import com.ms.security.utils.LogUtils;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;
import com.trello.rxlifecycle3.components.support.RxFragmentActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static android.graphics.Color.BLACK;

public class RxCommonUtils {

    /**
     * 倒计时秒数
     *
     * @param seconds 倒计时秒数
     * @param view    控件
     */

    public static void countDown(RxAppCompatActivity context, int seconds, Button view) {
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(seconds + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        LogUtils.E(aLong + "s--" + (seconds - aLong));
                        return seconds - aLong;
                    }
                })
                .compose(context.bindToLifecycle())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        LogUtils.E("disposable");
                        view.setEnabled(false);
                        view.setTextColor(Color.RED);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        LogUtils.E("Disposable");
                    }

                    @Override
                    public void onNext(Long aLong) {
                        view.setText(aLong + "s");
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.E("onError--" + e.getMessage());
                        view.setEnabled(true);
                        view.setTextColor(BLACK);
                        view.setText("发送验证码");
                    }

                    @Override
                    public void onComplete() {
                        view.setEnabled(true);
                        view.setTextColor(BLACK);
                        view.setText("发送验证码");
                    }
                });
    }

    /**
     * 多少时间内
     *
     * @param seconds
     * @param
     * @return
     */

    public static boolean returnTimeCount(RxFragmentActivity context, int seconds) {
        final boolean[] isbool = {false};
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(seconds + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        LogUtils.E(aLong + "s--" + (seconds - aLong));
                        return seconds - aLong;
                    }
                })
                .compose(context.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        LogUtils.E(aLong + "s");
                        if (aLong <= 0) {
                            isbool[0] = true;
                        } else {
                            isbool[0] = false;
                        }
                    }
                });
        return isbool[0];

    }

    /**
     * 输入框隔多少毫秒
     *
     * @param editText
     * @param milliseconds
     */
    public static void editSearch(EditText editText, int milliseconds) {
        RxTextView.textChanges(editText)
                .debounce(milliseconds, TimeUnit.MILLISECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                /**switchMap 将Observable发射的数据集合变换为Observables集合,然后只发射这些Observables最近发射的数据*/
                .switchMap(new Function<CharSequence, ObservableSource<List<String>>>() {
                    @Override
                    public ObservableSource<List<String>> apply(CharSequence charSequence) throws Exception {
                        /**根据字符charSequence大于0去 处理网络请求  并返回结果  这里用string集合表示*/
                        List<String> list = new ArrayList<>();
                        list.add("1");
                        list.add("2");
                        list.add("3");
                        list.add("4");
                        return Observable.just(list);
                    }
                })
                /**去请求网络  但不能保证是否是用户想要的数据  例如 输入1,12,返回可能是1搜索的数据*/
//                .flatMap(new Function<CharSequence, ObservableSource<List<String>>>() {
//                    @Override
//                    public ObservableSource<List<String>> apply(CharSequence charSequence) throws Exception {
//                        /**根据字符charSequence大于0去 处理网络请求  并返回结果  这里用string集合表示*/
//                        List<String> list = new ArrayList<>();
//                        list.add("1");
//                        list.add("2");
//                        list.add("3");
//                        list.add("4");
//                        return Observable.just(list);
//                    }
//                })
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(List<String> integer) {
                        for (String ss : integer) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    public static Observable<List<List<Bean>>> chooseData(Map<String, Bean> map) {
        //把map转换成list
        List<Bean> beans = new ArrayList<>();
        for (Bean b : map.values()) {
            beans.add(b);
        }
        return Observable.just(beans)
                .flatMap(new Function<List<Bean>, ObservableSource<List<List<Bean>>>>() {
                    @Override
                    public ObservableSource<List<List<Bean>>> apply(List<Bean> beans) throws Exception {
                        //string字段相同存入map
                        Map<String, String> mapstring = new HashMap<>();
                        for (Bean b : beans) {
                            mapstring.put(b.getString(), b.getString());
                        }
                        LogUtils.E(mapstring.toString());
                        //组合分类数据
                        List<List<Bean>> listList = new ArrayList<>();
                        for (Map.Entry<String, String> entry : mapstring.entrySet()) {
                            List<Bean> lisdan = new ArrayList<>();
                            for (Bean b : map.values()) {
                                if (entry.getKey().equals(b.getString())) {
                                    lisdan.add(b);
                                }
                            }
                            LogUtils.E(lisdan.toString());
                            listList.add(lisdan);
                        }
                        return Observable.just(listList);
                    }
                }).observeOn(AndroidSchedulers.mainThread());

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值