Androidのデータバインド(a)の基本的な使い方
Androidのデータバインディング(2)イベントハンドラ
Androidのデータバインディング(3)観測可能な
Androidのデータバインド(d)のカスタム属性を
Androidのデータバインディングを(5)バインディングカスタムビューの道
のAndroidデータバインディングを(6)のEditTextと結合TextChangedListener FocusChangeListener(紙)
以下のように設定するXML
<EditText
......
app:addTextChangedListener="@{vm.textWatcher}"
app:onFocusChangeListener="@{(view, hasFocus) -> vm.setText(((EditText)view).getText().toString(), hasFocus)}" />
次のようにViewModelには設定されています
public void setText(String text, boolean hasFocus) {
if (hasFocus) {
......
} else {
......
}
}
private TextWatcher textWatcher = new SimpleTextWatcher() {
@Override public void afterTextChanged(Editable s)
......
}
};
簡潔コードのSimpleTextWatcherを定義
public abstract class SimpleTextWatcher implements TextWatcher {
@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) {
}
}