AutoCompleteTextViewBindingAdapter.java revision 716ba89e7f459f49ea85070d4710c1d79d715298
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package android.databinding.adapters; 17 18import android.databinding.BindingAdapter; 19import android.databinding.BindingMethod; 20import android.databinding.BindingMethods; 21import android.databinding.adapters.AdapterViewBindingAdapter.OnItemSelected; 22import android.databinding.adapters.AdapterViewBindingAdapter.OnItemSelectedComponentListener; 23import android.databinding.adapters.AdapterViewBindingAdapter.OnNothingSelected; 24import android.widget.AutoCompleteTextView; 25import android.widget.AutoCompleteTextView.Validator; 26 27@BindingMethods({ 28 @BindingMethod(type = AutoCompleteTextView.class, attribute = "android:completionThreshold", method = "setThreshold"), 29 @BindingMethod(type = AutoCompleteTextView.class, attribute = "android:popupBackground", method = "setDropDownBackgroundDrawable"), 30 @BindingMethod(type = AutoCompleteTextView.class, attribute = "android:onDismiss", method = "setOnDismissListener"), 31 @BindingMethod(type = AutoCompleteTextView.class, attribute = "android:onItemClick", method = "setOnItemClickListener"), 32}) 33public class AutoCompleteTextViewBindingAdapter { 34 35 @BindingAdapter("android:fixText") 36 public static void setListener(AutoCompleteTextView view, FixText listener) { 37 setListener(view, listener, null); 38 } 39 40 @BindingAdapter("android:isValid") 41 public static void setListener(AutoCompleteTextView view, IsValid listener) { 42 setListener(view, null, listener); 43 } 44 45 @BindingAdapter({"android:fixText", "android:isValid"}) 46 public static void setListener(AutoCompleteTextView view, final FixText fixText, 47 final IsValid isValid) { 48 if (fixText == null && isValid == null) { 49 view.setValidator(null); 50 } else { 51 view.setValidator(new Validator() { 52 @Override 53 public boolean isValid(CharSequence text) { 54 if (isValid != null) { 55 return isValid.isValid(text); 56 } else { 57 return true; 58 } 59 } 60 61 @Override 62 public CharSequence fixText(CharSequence invalidText) { 63 if (fixText != null) { 64 return fixText.fixText(invalidText); 65 } else { 66 return invalidText; 67 } 68 } 69 }); 70 } 71 } 72 73 @BindingAdapter("android:onItemSelected") 74 public static void setListener(AutoCompleteTextView view, OnItemSelected listener) { 75 setListener(view, listener, null); 76 } 77 78 @BindingAdapter("android:onNothingSelected") 79 public static void setListener(AutoCompleteTextView view, OnNothingSelected listener) { 80 setListener(view, null, listener); 81 } 82 83 @BindingAdapter({"android:onItemSelected", "android:onNothingSelected"}) 84 public static void setListener(AutoCompleteTextView view, final OnItemSelected selected, 85 final OnNothingSelected nothingSelected) { 86 if (selected == null && nothingSelected == null) { 87 view.setOnItemSelectedListener(null); 88 } else { 89 view.setOnItemSelectedListener( 90 new OnItemSelectedComponentListener(selected, nothingSelected)); 91 } 92 } 93 94 public interface IsValid { 95 boolean isValid(CharSequence text); 96 } 97 98 public interface FixText { 99 CharSequence fixText(CharSequence invalidText); 100 } 101} 102