Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Last active December 3, 2015 15:49
Show Gist options
  • Save t-kashima/8be3ee927fedd8b9ad3d to your computer and use it in GitHub Desktop.
Save t-kashima/8be3ee927fedd8b9ad3d to your computer and use it in GitHub Desktop.
RxJavaで入力の状態によって条件を分ける
/**
* ボタンを押した時に入力が空の時は何もしないで
* 入力はあるが20文字より多い時はエラーを表示する
*/
mButton.setOnClickListener(v ->
Observable.just(mEditText.getText().toString())
.filter(text -> !text.isEmpty())
.filter(text -> {
if (20 < text.length()) {
throw new IllegalStateException("Invalid your text length.");
}
return true;
}).subscribe(text ->
Log.d("MainActivity", "OK, Your text is " + text)
, error ->
Log.e("MainActivity", "NG, " + error.getMessage())
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment