Android需求之RxJava2實現表單校驗(註冊登入)

cdAndroid發表於2020-11-13

一. 預覽效果

二. 開發需求

使用者註冊登入等表單校驗場景下,只有賬號和密碼內容都不為空的情況下,按鈕才可點選並實現按鈕背景變化。

三. 實現

1. 依賴

// rxbinding3 內部已新增rxjava依賴
implementation "com.jakewharton.rxbinding3:rxbinding:3.0.0"

2. Rxjava操作符之combineLatest

combineLatest 操作符用來將多個Observable發射的資料組裝起來然後在發射. 通過Func類來組裝多個Observable發射的資料, 等到最後一個Observable發射資料了, 然後把所有發射的資料交給Fun進行組合, 然後把組合後的資料發射出去.

3. 具體實現

話不多說,直接上程式碼:

kotlin :

// 1.繼承TextWatcher
class EditTextMonitor(private val mPublishSubject: PublishSubject<String>) : TextWatcher {
    override fun beforeTextChanged(
        s: CharSequence,
        start: Int,
        count: Int,
        after: Int
    ) {}

    override fun onTextChanged(
        s: CharSequence,
        start: Int,
        before: Int,
        count: Int
    ) {}

    override fun afterTextChanged(s: Editable) {
        mPublishSubject.onNext(s.toString())
    }
}
		// 處理登陸按鈕是否可點選
        val mAccountSubject = PublishSubject.create<String>()
        val mPasswordSubject = PublishSubject.create<String>()
		// 	etAccount etPwd 賬號和密碼輸入框	
		etAccount.addTextChangedListener(EditTextMonitor(mAccountSubject))
        etPwd.addTextChangedListener(EditTextMonitor(mPasswordSubject))
        Observable.combineLatest(mAccountSubject, mPasswordSubject,
            BiFunction { name: String, password: String ->
                val nameLen = name.length
                val passwordLen = password.length
                nameLen > 0 && passwordLen > 0
            }
        ).subscribe { clickFlag: Boolean ->
            // 滿足以上nameLen > 0 && passwordLen > 0後
            if (clickFlag) {
            // btnLogin 登入按鈕
                btnLogin.apply {
                    isEnabled = true
                    setBackgroundResource(R.color.white)
                    setTextColor(Color.BLACK)
                }
            // 不滿足:
            } else {
                btnLogin.apply {
                    isEnabled = false
                    setBackgroundResource(R.color.white_cc)
                    setTextColor(Color.WHITE)
                }

            }
        }

Java:

(Java環境為1.8 支援lamda表示式)

public class EditTextMonitor implements TextWatcher {

    private PublishSubject<String> mPublishSubject;

    public EditTextMonitor(PublishSubject<String> publishSubject) {
        mPublishSubject = publishSubject;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        mPublishSubject.onNext(s.toString());
    }
}
		// 處理登陸按鈕是否可點選
        PublishSubject<String> mAccountSubject = PublishSubject.create();
        PublishSubject<String> mPasswordSubject = PublishSubject.create();
        edLoginAccount.addTextChangedListener(new EditTextMonitor(mAccountSubject));
        edLoginPwd.addTextChangedListener(new EditTextMonitor(mPasswordSubject));
        Observable.combineLatest(mAccountSubject, mPasswordSubject, (name, password) -> {
            int nameLen = name.length();
            int passwordLen = password.length();
            return nameLen > 0 && passwordLen > 0;
        }).subscribe(clickFlag -> {
            if (clickFlag) {
                btnLogin.setEnabled(true);
            } else {
                btnLogin.setEnabled(false);
            }
        });

相關文章