/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package my.home.lehome.view; import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.widget.AutoCompleteTextView; /** * Created by legendmohe on 15/2/14. */ public class DelayAutoCompleteTextView extends AutoCompleteTextView { private static final int MESSAGE_TEXT_CHANGED = 100; private static final int DEFAULT_AUTOCOMPLETE_DELAY = 300; private int mAutoCompleteDelay = DEFAULT_AUTOCOMPLETE_DELAY; private boolean autoCompleteEnable = true; private boolean canShowDropdown = true; private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { DelayAutoCompleteTextView.super.performFiltering((CharSequence) msg.obj, msg.arg1); } }; public DelayAutoCompleteTextView(Context context, AttributeSet attrs) { super(context, attrs); } public void setAutoCompleteDelay(int autoCompleteDelay) { mAutoCompleteDelay = autoCompleteDelay; } public void performFiltering(CharSequence text) { if (!autoCompleteEnable) return; DelayAutoCompleteTextView.super.performFiltering(text, 0); } @Override protected void performFiltering(CharSequence text, int keyCode) { if (!autoCompleteEnable) return; mHandler.removeMessages(MESSAGE_TEXT_CHANGED); mHandler.sendMessageDelayed(mHandler.obtainMessage(MESSAGE_TEXT_CHANGED, text), mAutoCompleteDelay); } // @Override // protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { // super.onFocusChanged(focused, direction, previouslyFocusedRect); // if (focused && getAdapter() != null) { // performFiltering(getText(), 0); // } // } // @Override // public boolean enoughToFilter() { // return true; // } @Override public void showDropDown() { if (!canShowDropdown) return; super.showDropDown(); } @Override public void onFilterComplete(int count) { super.onFilterComplete(count); if (!isPopupShowing()) showDropDown(); } @Override protected void replaceText(CharSequence text) { // must override onItemClickListener to set selected text // super.replaceText(text); } public boolean isAutoCompleteEnable() { return autoCompleteEnable; } public void setAutoCompleteEnable(boolean autoCompleteEnable) { this.autoCompleteEnable = autoCompleteEnable; } public boolean isCanShowDropdown() { return canShowDropdown; } public void setCanShowDropdown(boolean canShowDropdown) { this.canShowDropdown = canShowDropdown; } }