2010-01-24 37 views
91

我使用AutoCompleteTextView,當用戶點擊它時,即使它沒有文字,我也想顯示建議 - 但setThreshold(0)setThreshold(1)完全相同 - 所以用戶必須輸入至少1個字符來顯示建議。Android:AutoCompleteTextView在沒有文字輸入時顯示建議

+0

我正在做類似的事情! http://stackoverflow.com/questions/12854336/autocompletetextview-backed-by-cursorloader – toobsco42 2012-10-30 19:48:25

回答

117

這是documented behavior:「當閾值小於或等於0時,應用閾值1。」。

您可以通過showDropDown()手動顯示下拉列表,因此也許您可以安排在需要時顯示它。或者,子類AutoCompleteTextView並覆蓋enoughToFilter(),始終返回true

+4

showDropDown()似乎在onClickListener上運行良好,但子類的東西沒有工作,直到用戶輸入一個字母,並dels back.But而不僅僅是onClick ... – amj 2013-02-20 18:15:51

+2

這個工作原理與OnFocusChangeListener完美結合,當視圖獲得焦點時調用showDropDown()。 – Grishka 2014-02-06 17:55:15

+0

showDropDown()完美無缺! – 2016-05-24 07:35:47

92

這是我的課 - 我稱之爲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 ... /> 
+10

太棒了!我還想指出,在佈局XML文件中,您必須將''更改爲''。我忘記了一些時間:) – 2012-05-23 04:40:57

+3

偉大的類 - 只有在onFocusChanged方法中,將「if(focused)」更改爲「if(focused && getAdapter()!= null)」。 – 2013-12-14 23:58:36

+0

@JulesColle謝謝,編輯 – 2014-12-26 14:38:00

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; 
    } 
} 
14

Destil的代碼工作好了的時候,只有一個InstantAutoComplete對象。 它沒有與兩個雖然工作 - 不知道爲什麼。但是,當我把showDropDown()(就像CommonsWare決定)爲onFocusChanged()這樣的:

@Override 
protected void onFocusChanged(boolean focused, int direction, 
     Rect previouslyFocusedRect) { 
    super.onFocusChanged(focused, direction, previouslyFocusedRect); 
    if (focused) { 
     performFiltering(getText(), 0); 
     showDropDown(); 
    } 
} 

它解決了這個問題。

這只是兩個答案正確結合,但我希望它可以節省一些人一些時間。

+1

您的補充幫助,但如果InstantAutoComplete中有文本並且屏幕方向已更改,則會出現錯誤。我使用窗口可見性檢查來修復它,我在這裏發佈了新代碼:https://gist.github.com/furycomptuers/4961368 – FuryComputers 2013-02-15 16:25:00

4

您可以使用onFocusChangeListener;

TCKimlikNo.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (hasFocus) { 
       TCKimlikNo.showDropDown(); 

      } 

     } 
    }); 
36

最簡單的方法:

只需使用setOnTouchListener和showDropDown()

AutoCompleteTextView text; 
..... 
..... 
text.setOnTouchListener(new View.OnTouchListener(){ 
    @Override 
    public boolean onTouch(View v, MotionEvent event){ 
     text.showDropDown(); 
     return false; 
    } 
}); 
+0

爲了使這更好用,如果(!text.isPopupShowing()){ text .showDropDown(); } – 2014-12-15 12:57:34

+6

並不常見,但如果用戶沒有觸摸這個EditText,這將不起作用。例如,當使用帶按鈕的遙控器(例如Android TV)時。 – 2014-12-31 00:27:51

+0

你應該使用setOnFocusChanged。有人可以有鍵盤和按Tab鍵或使用鼠標和觸摸監聽器不會被調用。 – barwnikk 2015-08-29 04:00:05

5

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(""); 
     } 
    } 
} 
+0

,但這也意味着文本將被重置(雖然通常情況下通常很好)。 – 2014-12-31 00:29:36

5

適配器最初不執行過濾。
未執行過濾時,下拉列表爲空。
因此您可能必須先進行過濾。

要做到這一點,你可以調用filter()添加完條目後:

adapter.add("a1"); 
adapter.add("a2"); 
adapter.add("a3"); 
adapter.getFilter().filter(null); 
0

嘗試

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; 
      } 
     }): 
2

只需撥打觸摸此方法,或者點擊autoCompleteTextView或者事件你要。

autoCompleteTextView.showDropDown() 
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); 
    } 
} 

0

只需粘貼到您的onCreate方法在Java中

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"/> 

,打造下價值觀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> 

,你是好去。

0

七年後,夥計們,問題保持不變。這裏有一個功能,強制這個愚蠢的彈出窗口在任何情況下顯示自己。您只需將適配器設置爲AutoCompleteTextView,向其中添加一些數據,然後隨時調用函數即可。

致信@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) 
     } 
    } 
} 
相關問題