Android:AutoCompleteTextView 在未输入文本时显示建议
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2126717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: AutoCompleteTextView show suggestions when no text entered
提问by fhucho
I am using AutoCompleteTextView
, when the user clicks on it, I want to show suggestions even if it has no text - but setThreshold(0)
works exactly the same as setThreshold(1)
- so the user has to enter at least 1 character to show the suggestions.
我正在使用AutoCompleteTextView
,当用户单击它时,即使它没有文本,我也想显示建议 - 但setThreshold(0)
工作原理与setThreshold(1)
- 所以用户必须输入至少 1 个字符才能显示建议。
回答by CommonsWare
This is documented behavior:
这是记录在案的行为:
When
threshold
is less than or equals 0, a threshold of 1 is applied.
当
threshold
小于或等于 0 时,应用阈值 1。
You can manually show the drop-down via showDropDown()
, so perhaps you can arrange to show it when you want. Or, subclass AutoCompleteTextView
and override enoughToFilter()
, returning true
all of time.
您可以通过 手动显示下拉菜单showDropDown()
,因此也许您可以安排在需要时显示它。或者,子类化AutoCompleteTextView
和覆盖enoughToFilter()
,一直返回true
。
回答by David Vávra
Here is my class InstantAutoComplete. It's something between AutoCompleteTextView
and Spinner
.
这是我的班级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);
}
}
}
Use it in your xml like this:
在您的 xml 中使用它,如下所示:
<your.namespace.InstantAutoComplete ... />
回答by user1913469
Easiest way:
最简单的方法:
Just use setOnTouchListener and showDropDown()
只需使用 setOnTouchListener 和 showDropDown()
AutoCompleteTextView text;
.....
.....
text.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
text.showDropDown();
return false;
}
});
回答by alex
Destil's code works just great when there is only one InstantAutoComplete
object. It didn't work with two though- no idea why. But when I put showDropDown()
(just like CommonsWare advised) into onFocusChanged()
like this:
当只有一个InstantAutoComplete
对象时,Destil 的代码工作得很好。虽然它不适用于两个- 不知道为什么。但是当我把showDropDown()
(就像CommonsWare建议的那样)变成onFocusChanged()
这样时:
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
performFiltering(getText(), 0);
showDropDown();
}
}
it solved the problem.
它解决了这个问题。
It is just the two answers properly combined, but I hope it may save somebody some time.
这只是将两个答案正确组合在一起,但我希望它可以节省一些时间。
回答by david m lee
The adapter does not perform filtering initially.
When the filtering is not performed, the dropdown list is empty.
so you might have to get the filtering going initially.
适配器最初不执行过滤。
不进行过滤时,下拉列表为空。
所以您可能必须首先进行过滤。
To do so, you can invoke filter()
after you finish adding the entries:
为此,您可以filter()
在添加完条目后调用:
adapter.add("a1");
adapter.add("a2");
adapter.add("a3");
adapter.getFilter().filter(null);
回答by G?ksel Güren
You can use onFocusChangeListener;
您可以使用 onFocusChangeListener;
TCKimlikNo.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
TCKimlikNo.showDropDown();
}
}
});
回答by Colin Stewart
Destil's answer above almost works, but has one subtle bug. When the user first gives focus to the field it works, however if they leave and then return to the field it will not show the drop down because the value of mPopupCanBeUpdated will still be false from when it was hidden. The fix is to change the onFocusChanged method to:
Destil 上面的回答几乎有效,但有一个微妙的错误。当用户第一次将焦点放在它可以工作的字段上时,但是如果他们离开然后返回该字段,它不会显示下拉菜单,因为 mPopupCanBeUpdated 的值从它隐藏时起仍然是假的。解决方法是将 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("");
}
}
}
回答by sanjeev vishnoi
To make CustomAutoCompleteTextView. 1. override setThreshold,enoughToFilter,onFocusChanged method
制作 CustomAutoCompleteTextView。1. 覆盖 setThreshold,enoughToFilter,onFocusChanged 方法
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;
}
}
回答by Rafayel Pogosyan
try it
尝试一下
searchAutoComplete.setThreshold(0);
searchAutoComplete.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {//cut last probel
if (charSequence.length() > 1) {
if (charSequence.charAt(charSequence.length() - 1) == ' ') {
searchAutoComplete.setText(charSequence.subSequence(0, charSequence.length() - 1));
searchAutoComplete.setSelection(charSequence.length() - 1);
}
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
//when clicked in autocomplete text view
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.header_search_etv:
if (searchAutoComplete.getText().toString().length() == 0) {
searchAutoComplete.setText(" ");
}
break;
}
}):
回答by Dalvinder Singh
Just call this method on touch or click event of autoCompleteTextView or where you want.
只需在 autoCompleteTextView 的触摸或单击事件或您想要的地方调用此方法。
autoCompleteTextView.showDropDown()