|
| 1 | +package com.vogella.android.testapp; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.databinding.BaseObservable; |
| 5 | +import android.databinding.Bindable; |
| 6 | +import android.databinding.BindingAdapter; |
| 7 | +import android.databinding.ObservableField; |
| 8 | +import android.text.Editable; |
| 9 | +import android.text.TextWatcher; |
| 10 | +import android.view.View; |
| 11 | +import android.widget.EditText; |
| 12 | +import android.widget.Toast; |
| 13 | + |
| 14 | +import java.util.Objects; |
| 15 | + |
| 16 | +public class MainActivityHandlers extends BaseObservable { |
| 17 | + public ObservableField<String> textFieldValue = new ObservableField<>(); |
| 18 | + private Context context; |
| 19 | + |
| 20 | + public void onClick(View view) { |
| 21 | + context = view.getContext(); |
| 22 | + Toast.makeText(context, textFieldValue.get(), Toast.LENGTH_SHORT).show(); |
| 23 | + } |
| 24 | + |
| 25 | + @Bindable |
| 26 | + public String getName() { |
| 27 | + return textFieldValue.get(); |
| 28 | + } |
| 29 | + |
| 30 | + public void setName(String name) { |
| 31 | + textFieldValue.set(name); |
| 32 | + notifyPropertyChanged(com.vogella.android.testapp.BR.name); |
| 33 | + } |
| 34 | + |
| 35 | + public final TextWatcher textWatcher = new TextWatcher() { |
| 36 | + @Override |
| 37 | + public void afterTextChanged(Editable s) { |
| 38 | + if(!s.toString().equalsIgnoreCase(getName())) |
| 39 | + setName(s.toString()); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void beforeTextChanged(CharSequence s, int start, int count, int after) {} |
| 44 | + |
| 45 | + @Override |
| 46 | + public void onTextChanged(CharSequence s, int start, int before, int count) {} |
| 47 | + }; |
| 48 | + |
| 49 | +// // Add this to avoid that the setText method is called in case the model changes |
| 50 | +// @BindingAdapter("binding") |
| 51 | +// public static void bindEditText(EditText editText, CharSequence value) { |
| 52 | +// if (!editText.getText().toString().equals(value.toString())) { |
| 53 | +// editText.setText(value); |
| 54 | +// } |
| 55 | +// } |
| 56 | +} |
0 commit comments