![](/img/trans.png)
[英]AutoCompleteTextView doesn't show suggestions in Android 2.3.5
[英]Android: AutoCompleteTextView show suggestions when no text entered
我正在使用AutoCompleteTextView
,當用戶單擊它時,即使它沒有文本,我也想顯示建議 - 但setThreshold(0)
工作方式與setThreshold(1)
完全相同 - 因此用戶必須輸入至少 1 個字符才能顯示建議。
這是記錄在案的行為:
當
threshold
小於或等於 0 時,應用閾值 1。
您可以通過showDropDown()
手動顯示下拉列表,因此也許您可以安排在需要時顯示它。 或者, enoughToFilter()
AutoCompleteTextView
並覆蓋enoughToFilter()
, enoughToFilter()
返回true
。
這是我的班級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 ... />
最簡單的方法:
只需使用 setOnTouchListener 和 showDropDown()
AutoCompleteTextView text;
.....
.....
text.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
text.showDropDown();
return false;
}
});
當只有一個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();
}
}
它解決了這個問題。
這只是將兩個答案正確組合在一起,但我希望它可以節省一些時間。
適配器最初不執行過濾。
不進行過濾時,下拉列表為空。
所以您可能必須首先進行過濾。
為此,您可以在添加完條目后調用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 的值從它隱藏時起仍然是假的。 解決方法是將 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("");
}
}
}
嘗試一下
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;
}
}):
只需在 autoCompleteTextView 的觸摸或單擊事件或您想要的地方調用此方法。
autoCompleteTextView.showDropDown()
制作 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;
}
}
在 FocusChangeListener 上,檢查
if (hasFocus) {
tvAutoComplete.setText(" ")
在您的過濾器中,只需修剪此值:
filter { it.contains(constraint.trim(), true) }
當您關注此視圖時,它將顯示所有建議。
這對我有用,偽代碼:
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);
}
}
只需將其粘貼到 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>
你很高興去。
七年后,伙計們,問題還是一樣。 這是一個具有強制該愚蠢彈出窗口在任何條件下顯示自己的函數的類。 您需要做的就是為您的 AutoCompleteTextView 設置一個適配器,向其中添加一些數據,並隨時調用showDropdownNow()
函數。
感謝@David Vá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)
}
}
}
對我來說,它是:
autoCompleteText.setThreshold(0);
做的伎倆。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.