我正在使用AutoCompleteTextView
,当用户点击它时,我想显示建议,即使它没有文字 - 但setThreshold(0)
的工作原理与setThreshold(1)
完全相同 - 所以用户必须输入至少1个字符才能显示建议。
答案 0 :(得分:139)
当
threshold
小于或等于0时,阈值为1 应用
您可以通过showDropDown()
手动显示下拉列表,因此您可以安排在需要时显示该下拉列表。或者,子类AutoCompleteTextView
并覆盖enoughToFilter()
,一直返回true
。
答案 1 :(得分:109)
这是我的班级 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 ... />
答案 2 :(得分:44)
最简单的方法:
只需使用setOnTouchListener和showDropDown()
AutoCompleteTextView text;
.....
.....
text.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
text.showDropDown();
return false;
}
});
答案 3 :(得分:15)
当只有一个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();
}
}
它解决了这个问题。
这两个答案恰好合并,但我希望它可以节省一些时间。
答案 4 :(得分:8)
适配器最初不执行过滤
未执行过滤时,下拉列表为空
所以你可能必须先进行过滤。
为此,您可以在添加完条目后调用filter()
:
adapter.add("a1");
adapter.add("a2");
adapter.add("a3");
adapter.getFilter().filter(null);
答案 5 :(得分:6)
上面的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("");
}
}
}
答案 6 :(得分:5)
您可以使用onFocusChangeListener;
TCKimlikNo.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
TCKimlikNo.showDropDown();
}
}
});
答案 7 :(得分:3)
制作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;
}
}
答案 8 :(得分:2)
试试吧
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;
}
}):
答案 9 :(得分:2)
只需在触摸时点击此方法或点击autoCompleteTextView的事件或您想要的位置。
autoCompleteTextView.showDropDown()
答案 10 :(得分:0)
这对我有用,伪代码:
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean enoughToFilter() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
performFiltering(getText(), 0);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
this.showDropDown();
return super.onTouchEvent(event);
}
}
答案 11 :(得分:0)
只需将其粘贴到Java中的onCreate方法
即可final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.Loc_names));
textView1 =(AutoCompleteTextView) findViewById(R.id.acT1);
textView1.setAdapter(arrayAdapter);
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
textView1.setMaxLines(5);
textView1.showDropDown();
}
});
这是你的Xml文件......
<AutoCompleteTextView
android:layout_width="200dp"
android:layout_height="30dp"
android:hint="@string/select_location"
android:id="@+id/acT1"
android:textAlignment="center"/>
在Values ...
下的string.xml中创建一个数组<string-array name="Loc_names">
<item>Pakistan</item>
<item>Germany</item>
<item>Russia/NCR</item>
<item>China</item>
<item>India</item>
<item>Sweden</item>
<item>Australia</item>
</string-array>
你很高兴。
答案 12 :(得分:0)
七年后,伙计们,问题保持不变。这是一个带有函数的类,它强制愚蠢的弹出窗口在任何条件下显示自己。您需要做的就是将适配器设置为AutoCompleteTextView,向其中添加一些数据,并随时调用showDropdownNow()
函数。
@DavidVávra的积分。这是基于他的代码。
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)
}
}
}
答案 13 :(得分:0)
在FocusChangeListener上,检查
bottom: 0
在过滤器中,只需修整此值:
if (hasFocus) {
tvAutoComplete.setText(" ")
,当您专注于此视图时,它将显示所有建议。