summaryrefslogtreecommitdiff
path: root/extensions/baseAdapters/src/main/java/android/databinding/adapters/TextViewBindingAdapter.java
diff options
context:
space:
mode:
authorGeorge Mount <mount@google.com>2015-06-17 16:09:52 -0700
committerGeorge Mount <mount@google.com>2015-06-23 15:18:43 -0700
commit716ba89e7f459f49ea85070d4710c1d79d715298 (patch)
tree256cfa9eb7d085f88d27001d35bcea9728a35596 /extensions/baseAdapters/src/main/java/android/databinding/adapters/TextViewBindingAdapter.java
parentaf84cb304c158381a8bf0f0ac1c37c5a2ad04bfb (diff)
downloaddata-binding-716ba89e7f459f49ea85070d4710c1d79d715298.tar.gz
Support calling listener methods without interfaces.
Bug 21594573 It is convenient to be able to assign event listeners by just referencing a method, similar to the way onClick="handler" works. This adds a whole lot of listeners for the framework. Additional listeners must be added for support library components. This isn't perfect in resolving listeners. Perfect resolution requires that each expression is evaluated in its own context within the binding statement. If, for example, the same method name is used for a listener and an accessor, we will assume that the listener is used always and there will be a compilation failure. Change-Id: If4705122b67a451430451b6e7d890eb813af1c5c
Diffstat (limited to 'extensions/baseAdapters/src/main/java/android/databinding/adapters/TextViewBindingAdapter.java')
-rw-r--r--extensions/baseAdapters/src/main/java/android/databinding/adapters/TextViewBindingAdapter.java107
1 files changed, 98 insertions, 9 deletions
diff --git a/extensions/baseAdapters/src/main/java/android/databinding/adapters/TextViewBindingAdapter.java b/extensions/baseAdapters/src/main/java/android/databinding/adapters/TextViewBindingAdapter.java
index 6b653719..01ea0069 100644
--- a/extensions/baseAdapters/src/main/java/android/databinding/adapters/TextViewBindingAdapter.java
+++ b/extensions/baseAdapters/src/main/java/android/databinding/adapters/TextViewBindingAdapter.java
@@ -20,8 +20,10 @@ import android.databinding.BindingMethod;
import android.databinding.BindingMethods;
import android.graphics.drawable.Drawable;
import android.os.Build;
+import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
+import android.text.TextWatcher;
import android.text.method.DialerKeyListener;
import android.text.method.DigitsKeyListener;
import android.text.method.KeyListener;
@@ -31,16 +33,19 @@ import android.util.Log;
import android.util.TypedValue;
import android.widget.TextView;
+import com.android.databinding.library.baseAdapters.R;
+
@BindingMethods({
- @BindingMethod(type = android.widget.TextView.class, attribute = "android:autoLink", method = "setAutoLinkMask"),
- @BindingMethod(type = android.widget.TextView.class, attribute = "android:drawablePadding", method = "setCompoundDrawablePadding"),
- @BindingMethod(type = android.widget.TextView.class, attribute = "android:editorExtras", method = "setInputExtras"),
- @BindingMethod(type = android.widget.TextView.class, attribute = "android:inputType", method = "setRawInputType"),
- @BindingMethod(type = android.widget.TextView.class, attribute = "android:scrollHorizontally", method = "setHorizontallyScrolling"),
- @BindingMethod(type = android.widget.TextView.class, attribute = "android:textAllCaps", method = "setAllCaps"),
- @BindingMethod(type = android.widget.TextView.class, attribute = "android:textColorHighlight", method = "setHighlightColor"),
- @BindingMethod(type = android.widget.TextView.class, attribute = "android:textColorHint", method = "setHintTextColor"),
- @BindingMethod(type = android.widget.TextView.class, attribute = "android:textColorLink", method = "setLinkTextColor"),
+ @BindingMethod(type = TextView.class, attribute = "android:autoLink", method = "setAutoLinkMask"),
+ @BindingMethod(type = TextView.class, attribute = "android:drawablePadding", method = "setCompoundDrawablePadding"),
+ @BindingMethod(type = TextView.class, attribute = "android:editorExtras", method = "setInputExtras"),
+ @BindingMethod(type = TextView.class, attribute = "android:inputType", method = "setRawInputType"),
+ @BindingMethod(type = TextView.class, attribute = "android:scrollHorizontally", method = "setHorizontallyScrolling"),
+ @BindingMethod(type = TextView.class, attribute = "android:textAllCaps", method = "setAllCaps"),
+ @BindingMethod(type = TextView.class, attribute = "android:textColorHighlight", method = "setHighlightColor"),
+ @BindingMethod(type = TextView.class, attribute = "android:textColorHint", method = "setHintTextColor"),
+ @BindingMethod(type = TextView.class, attribute = "android:textColorLink", method = "setLinkTextColor"),
+ @BindingMethod(type = TextView.class, attribute = "android:onEditorAction", method = "setOnEditorActionListener"),
})
public class TextViewBindingAdapter {
@@ -280,4 +285,88 @@ public class TextViewBindingAdapter {
public static void setTextSize(TextView view, float size) {
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
+
+ @BindingAdapter("android:afterTextChanged")
+ public static void setListener(TextView view, AfterTextChanged after) {
+ setListener(view, null, null, after);
+ }
+
+ @BindingAdapter("android:beforeTextChanged")
+ public static void setListener(TextView view, BeforeTextChanged before) {
+ setListener(view, before, null, null);
+ }
+
+ @BindingAdapter("android:onTextChanged")
+ public static void setListener(TextView view, OnTextChanged onTextChanged) {
+ setListener(view, null, onTextChanged, null);
+ }
+
+ @BindingAdapter({"android:beforeTextChanged", "android:afterTextChanged"})
+ public static void setListener(TextView view, final BeforeTextChanged before,
+ final AfterTextChanged after) {
+ setListener(view, before, null, after);
+ }
+
+ @BindingAdapter({"android:beforeTextChanged", "android:onTextChanged"})
+ public static void setListener(TextView view, final BeforeTextChanged before,
+ final OnTextChanged on) {
+ setListener(view, before, on, null);
+ }
+
+ @BindingAdapter({"android:onTextChanged", "android:afterTextChanged"})
+ public static void setListener(TextView view,final OnTextChanged on,
+ final AfterTextChanged after) {
+ setListener(view, null, on, after);
+ }
+
+ @BindingAdapter({"android:beforeTextChanged", "android:onTextChanged", "android:afterTextChanged"})
+ public static void setListener(TextView view, final BeforeTextChanged before,
+ final OnTextChanged on, final AfterTextChanged after) {
+ final TextWatcher newValue;
+ if (before == null && after == null && on == null) {
+ newValue = null;
+ } else {
+ newValue = new TextWatcher() {
+ @Override
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+ if (before != null) {
+ before.beforeTextChanged(s, start, count, after);
+ }
+ }
+
+ @Override
+ public void onTextChanged(CharSequence s, int start, int before, int count) {
+ if (on != null) {
+ on.onTextChanged(s, start, before, count);
+ }
+ }
+
+ @Override
+ public void afterTextChanged(Editable s) {
+ if (after != null) {
+ after.afterTextChanged(s);
+ }
+ }
+ };
+ }
+ final TextWatcher oldValue = ListenerUtil.trackListener(view, newValue, R.id.textWatcher);
+ if (oldValue != null) {
+ view.removeTextChangedListener(oldValue);
+ }
+ if (newValue != null) {
+ view.addTextChangedListener(newValue);
+ }
+ }
+
+ public interface AfterTextChanged {
+ void afterTextChanged(Editable s);
+ }
+
+ public interface BeforeTextChanged {
+ void beforeTextChanged(CharSequence s, int start, int count, int after);
+ }
+
+ public interface OnTextChanged {
+ void onTextChanged(CharSequence s, int start, int before, int count);
+ }
}