我正在使用AutoCompleteTextView
,当用户点击它时,即使没有输入文本,我也希望显示建议 - 但是setThreshold(0)
的效果与setThreshold(1)
完全相同 - 因此用户必须至少输入1个字符才能显示建议。
我正在使用AutoCompleteTextView
,当用户点击它时,即使没有输入文本,我也希望显示建议 - 但是setThreshold(0)
的效果与setThreshold(1)
完全相同 - 因此用户必须至少输入1个字符才能显示建议。
这是已记录的行为:
当
threshold
小于或等于0时,应用1的阈值。
您可以通过showDropDown()
手动显示下拉菜单,因此当您需要时可以安排显示它。或者,子类化AutoCompleteTextView
并覆盖enoughToFilter()
,始终返回true
。
afterTextChanged
中,当 .getText().toString().length() == 0
时,showDropDown()
方法无法正常工作? - Prabs这是我的类InstantAutoComplete。它介于AutoCompleteTextView
和Spinner
之间。
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;
public class InstantAutoComplete extends AutoCompleteTextView {
public InstantAutoComplete(Context context) {
super(context);
}
public InstantAutoComplete(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@Override
public boolean enoughToFilter() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused && getAdapter() != null) {
performFiltering(getText(), 0);
}
}
}
在您的xml中使用它,就像这样:
<your.namespace.InstantAutoComplete ... />
<AutoCompleteTextView ... />
更改为<your.namespace.InstantAutoComplete ... />
。我浪费了一些时间才弄明白这一点 :) - Jules Colleandroidx.appcompat.widget.AppCompatAutoCompleteTextView
。 - Mahmudul Hasan Shohag最简单的方法:
只需使用setOnTouchListener并调用showDropDown()
AutoCompleteTextView text;
.....
.....
text.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
text.showDropDown();
return false;
}
});
Destil的代码在只有一个InstantAutoComplete
对象时非常好用。但是当有两个对象时,它却无法工作-我不知道为什么。但是当我像CommonsWare建议的那样,在onFocusChanged()
中加入了showDropDown()
时,它就可以工作了:
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
performFiltering(getText(), 0);
showDropDown();
}
}
问题已得到解决。
这只是两个答案的正确结合,但我希望它能节省某些人的时间。
filter()
:adapter.add("a1");
adapter.add("a2");
adapter.add("a3");
adapter.getFilter().filter(null);
你可以使用onFocusChangeListener;
TCKimlikNo.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
TCKimlikNo.showDropDown();
}
}
});
Destil的答案几乎可以,但有一个微妙的错误。当用户首次聚焦于该字段时,它可以工作,但是如果他们离开并返回该字段,则不会显示下拉列表,因为mPopupCanBeUpdated的值仍将从隐藏时的false保持不变。修复方法是更改onFocusChanged方法:
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
if (getText().toString().length() == 0) {
// We want to trigger the drop down, replace the text.
setText("");
}
}
}
autoCompleteTextView.showDropDown()
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
private int myThreshold;
public CustomAutoCompleteTextView (Context context) {
super(context);
}
public CustomAutoCompleteTextView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomAutoCompleteTextView (Context context, AttributeSet attrs) {
super(context, attrs);
}
//set threshold 0.
public void setThreshold(int threshold) {
if (threshold < 0) {
threshold = 0;
}
myThreshold = threshold;
}
//if threshold is 0 than return true
public boolean enoughToFilter() {
return true;
}
//invoke on focus
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
//skip space and backspace
super.performFiltering("", 67);
// TODO Auto-generated method stub
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
protected void performFiltering(CharSequence text, int keyCode) {
// TODO Auto-generated method stub
super.performFiltering(text, keyCode);
}
public int getThreshold() {
return myThreshold;
}
}
showDropdownNow()
函数即可。import android.content.Context
import android.util.AttributeSet
import android.widget.AutoCompleteTextView
class InstantAutoCompleteTextView : AutoCompleteTextView {
constructor(context: Context) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
override fun enoughToFilter(): Boolean {
return true
}
fun showDropdownNow() {
if (adapter != null) {
// Remember a current text
val savedText = text
// Set empty text and perform filtering. As the result we restore all items inside of
// a filter's internal item collection.
setText(null, true)
// Set back the saved text and DO NOT perform filtering. As the result of these steps
// we have a text shown in UI, and what is more important we have items not filtered
setText(savedText, false)
// Move cursor to the end of a text
setSelection(text.length)
// Now we can show a dropdown with full list of options not filtered by displayed text
performFiltering(null, 0)
}
}
}