在使用系统默认的autoCompleteTextView的时候,发现一些问题:

  1. 只有在输入2个字符(当然这个最少的响应字符数量可以通过setThreshold()方法做调整,最小为1)或者以上的时候才会弹出备选答案,也就是输入框下面那个droplist.
  2. 当没有可以匹配输入单词的候选词的时候,droplist会消失掉。

可以发现,google自带的search功能,包括market上的search功能是没有以上两个问题的,同时google也不可能另外搞个控件来实现这么相似的功能,也就是说,autoCompleteTextView是肯定可以实现类似的功能的。

对于第一个问题,我通过在Activity的onPostResume()中强制调用showDropdown()方法,实现了在界面出现的时候能droplist能够出现,但是单击输入框之后,droplist还是消失了。好吧,单击输入框是为了呼出软键盘,我又继续在onPostResume()中添加了让软键盘弹出的代码。但是,当拖动droplist的时候...软键盘消失了。这个时候又不得不点输入框来callout软键盘。

废话了这么多...归根结底,如何让droplist一直显示?

找遍了AutoCompleteTextView的所有方法,都没有找到任何能够设置让droplist一直显示的方法。又有了另外一个思路,能否通过重写某些方法来实现?

尝试了重写dissmissDropDown()方法,让它啥都不做,不行。尝试重写enoughToFilter(),让它始终返回true,依旧不行。意外出现了,突然发现源代码里居然有一个名为setDropDownAlwaysVisible()的方法!

喜出望外,难道之前眼花了没看到有这个方法?又把AutoCompleteTextView的方法列表翻了一遍,依旧没有!再看源代码的时候,突然发现setDropDownAlwaysVisible()的注释是:


view plain copy to clipboard print ?

1. /**
2. * Like {@link #setText(CharSequence)}, except that it can disable filtering.
3. *
4. * @param filter If false, no filtering will be performed
5. *        as a result of this call.
6. * 
7. * @hide Pending API council approval.
8. */

明了明了,这个方法被google给隐藏掉了。

继续废话了这么多...终于进入正题了...

那么如何调用这个被隐藏掉了的方法呢?

AutoCompleteTextView的路径是 android.widget.AutoCompleteTextView; 在src目录下,建立一个相同的的android.widget目录,在该目录下也添加一个AutoCompleteTextView. 这个时候,在Eclipse中使用的AutoCompleteTextView顿时飘出一片红字,这也意味着在代码中使用的AutoCOmpleteTextView已经被新建的这个AutoCompleteTextView给取代掉了。

开始第二步的工作,为新的AutoCompleteTextView添上构造器,并且添上想要使用的方法,例如在这里,将要使用到setAdapter(),setDropDownAlwaysVisible(),setThreshold(),showDropDown()等方法,记得把参数什么都写进去,同时,这些方法里不需要写任何东西,空方法就行,它们的唯一作用就是保证编译通过。

运行程序,发现droplist出现之后无论干什么,也无论list里有没有内容,都不再消失了。问题解决。

这个方法的原理是,当系统发现SDK中已经实现的方法时,会优先调用系统方法,也就是说在程序运行时将不会调用我们自己添进去的方法,而是调用系统控件中的方法,哪怕它是隐藏的。

以下是用到的代码:


view plain copy to clipboard print ?

1. public
 
class
 AutoCompleteTextView 
extends
 EditText 
implements
 Filter.FilterListener {  
2.   
3. public
 AutoCompleteTextView(Context context) {  
4. super
(context);  
5.     }  
6.       
7. public
 AutoCompleteTextView(Context context, AttributeSet attrs) {  
8. super
(context, attrs);  
9.     }  
10.   
11. public
 AutoCompleteTextView(Context context, AttributeSet attrs,  
12. int
 defStyle) {  
13. super
(context, attrs, defStyle);  
14.     }  
15.   
16. public
 <T 
extends
 ListAdapter & Filterable> 
void
 setAdapter(T adapter) {  
17.     }  
18.   
19. public
 
void
 setOnItemClickListener(AdapterView.OnItemClickListener l) {  
20.     }  
21.       
22. public
 
void
 setDropDownAlwaysVisible(
boolean
 dropDownAlwaysVisible) {  
23.        }  
24.   
25. @Override
  
26. public
 
void
 onFilterComplete(
int
 count) {  
27.           
28.     }  
29.   
30. public
 
void
 setThreshold(
int
 threshold) {  
31.        }  
32.       
33. public
 
boolean
 enoughToFilter() {  
34. return
 
true
;  
35.     }  
36.       
37. public
 
void
 showDropDownAfterLayout() {  
38.        }  
39.       
40. public
 
void
 showDropDown(){}  
41.       
42. }