Skip to content

Instantly share code, notes, and snippets.

@maxost
Created September 5, 2017 02:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxost/c6b1bf9f42868d25808a22fcad17abd3 to your computer and use it in GitHub Desktop.
Save maxost/c6b1bf9f42868d25808a22fcad17abd3 to your computer and use it in GitHub Desktop.
Kotlin: editText Observable extension in Android
fun EditText.toObservable(): Observable<String> {
return Observable.create({
val watcher = object : TextWatcher {
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
scream("aaa" + p0.toString())
it.onNext(p0.toString())
}
override fun afterTextChanged(p0: Editable?) { }
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { }
}
this.addTextChangedListener(watcher)
it.setCancellable { this.removeTextChangedListener(watcher) }
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment