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(value = {"android:fixText", "android:isValid"}, requireAll = false) 36 public static void setValidator(AutoCompleteTextView view, final FixText fixText, 37 final IsValid isValid) { 38 if (fixText == null && isValid == null) { 39 view.setValidator(null); 40 } else { 41 view.setValidator(new Validator() { 42 @Override 43 public boolean isValid(CharSequence text) { 44 if (isValid != null) { 45 return isValid.isValid(text); 46 } else { 47 return true; 48 } 49 } 50 51 @Override 52 public CharSequence fixText(CharSequence invalidText) { 53 if (fixText != null) { 54 return fixText.fixText(invalidText); 55 } else { 56 return invalidText; 57 } 58 } 59 }); 60 } 61 } 62 63 @BindingAdapter(value = {"android:onItemSelected", "android:onNothingSelected"}, 64 requireAll = false) 65 public static void setOnItemSelectedListener(AutoCompleteTextView view, 66 final OnItemSelected selected, final OnNothingSelected nothingSelected) { 67 if (selected == null && nothingSelected == null) { 68 view.setOnItemSelectedListener(null); 69 } else { 70 view.setOnItemSelectedListener( 71 new OnItemSelectedComponentListener(selected, nothingSelected, null)); 72 } 73 } 74 75 public interface IsValid { 76 boolean isValid(CharSequence text); 77 } 78 79 public interface FixText { 80 CharSequence fixText(CharSequence invalidText); 81 } 82} 83