Android源码库里面,这个类的 OnItemSelectedListener事件没有做处理!
所以在使用这个类时,OnItemClickListener有效果,而OnItemSelectedListener没有效果的!
解决方法:利用反射机制,进行设置;
代码如下:
[java] view plain copy /** * */ package com.telecom.surfing.tvcn.view; import java.lang.reflect.Field; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.ListPopupWindow; /** * 从写了 onKeyPreIme(),back时,只隐藏软键盘; * 新增了OnItemSelectedListener处理,Android默认没有做该方法的回调处理 * */ public class AutoCompleteTextView extends android.widget.AutoCompleteTextView{ private AdapterView.OnItemSelectedListener onItemSelectedListener=null; /** * @param context * @param attrs */ public AutoCompleteTextView(Context context, AttributeSet attrs) { super(context, attrs); initOnItemSelectedLister(); } /** * @param context */ public AutoCompleteTextView(Context context) { super(context); initOnItemSelectedLister(); } /** * @param context * @param attrs * @param defStyle */ public AutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initOnItemSelectedLister(); } /* (non-Javadoc) * @see android.widget.AutoCompleteTextView#onKeyPreIme(int, android.view.KeyEvent) */ @Override public boolean onKeyPreIme(int keyCode, KeyEvent event) { Log.d("KEy", "KEy->AutoCompleteTextView onKeyPreIme,keycode="+keyCode); InputMethodManager imm = (InputMethodManager)this.getContext().getSystemService(this.getContext().INPUT_METHOD_SERVICE); if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction()== android.view.KeyEvent.ACTION_UP){//当软键盘显示时,按back按钮时,只隐藏软键盘; if(imm.hideSoftInputFromWindow(this.getWindowToken(), 0)){ //imm.showSoftInput(textView,0); return true; } } return super.onKeyPreIme(keyCode, event); } private void initOnItemSelectedLister(){ try { Field mPopuoField = AutoCompleteTextView.class.getSuperclass().getDeclaredField("mPopup"); mPopuoField.setAccessible(true); ListPopupWindow mPopup = (ListPopupWindow) mPopuoField.get(this); mPopup.setOnItemSelectedListener(new PopWindOnItemSelectedListener()); mPopuoField.setAccessible(false); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } class PopWindOnItemSelectedListener implements AdapterView.OnItemSelectedListener{ /* (non-Javadoc) * @see android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView, android.view.View, int, long) */ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { if(onItemSelectedListener!=null){ onItemSelectedListener.onItemSelected(arg0, arg1, arg2, arg3); } } /* (non-Javadoc) * @see android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android.widget.AdapterView) */ @Override public void onNothingSelected(AdapterView<?> arg0) { if(onItemSelectedListener!=null){ onItemSelectedListener.onNothingSelected(arg0); } } } /** * @return the onItemSelectedListener */ public AdapterView.OnItemSelectedListener getOnItemSelectedListener() { return onItemSelectedListener; } /** * @param onItemSelectedListener the onItemSelectedListener to set */ public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener onItemSelectedListener) { this.onItemSelectedListener = onItemSelectedListener; } /* (non-Javadoc) * @see android.widget.AutoCompleteTextView#performFiltering(java.lang.CharSequence, int) */ @Override protected void performFiltering(CharSequence text, int keyCode) { //重写该方法后,输入字符时,就不会过滤; // super.performFiltering(text, keyCode); } }