0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

EditTextで文字の入力を検知する方法

Posted at

はじめに

今回はEditText内の文字が入力された時に動きを加えたいと思い、今回の方法を調べました
↓の記事を参考にしました

LiveData

↑の記事を参考にしましたが、それをLiveDataの中で使えるようにしました
今回はEditText内の文字が入力、削除されたことを検知し動きを加えたかったので
対象のEditTextに

android:afterTextChanged="@{viewModel.EditTextChanged}"

この一文を加え、DataBindingで指定しているViewModelに
EditTextChanged()という関数を作りました

fun EditTextChanged(editable: Editable) {}

あとはこの{}に文字が入力、削除された時にやりたい処理を追加するだけです

終わりに

今回は文字の変更された事を検知したかったのでXML内で

afterTextChanged

をつかいましたが、
参考にした↑の記事内にもある
beforeTextChanged(文字の変更前に呼ばれる)
onTextChanged(文字の変更中に呼ばれる)
この二つもそれぞれLiveDataを使ったXML内で使えるので状況に応じてうまく利用できればいいなと思います
その時にはそれぞれViewModelで呼び出すときの引数の違いに気をつけたいところですね!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?