2013-03-21 56 views
26

我有一個AutocompleteTextView,它工作正常。當我寫一個單詞時,它會顯示相關的結果,但是我想顯示所有項目而不用在AutocompleteTextView中編寫任何單詞。我怎樣才能做到這一點。顯示AutocompleteTextView中的所有項目而不寫入文本

+0

嘗試此鏈接代碼可以幫助你... 。 http://stackoverflow.com/questions/15224027/custom-autocompletevview-li ke-facebook-comment-field-in-android/15247212#15247212 – 2013-03-21 10:33:17

+0

這不是我想要的實際解決方案。不管怎樣,謝謝。 – androidcodehunter 2013-03-21 10:41:21

回答

56

您需要延長AutoCompleteTextView,

「當閾值小於或等於0,1的閾值是 施用。」。

setThreshold

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) { 
      performFiltering(getText(), 0); 
     } 
    } 

} 

在XML

<AutoCompleteTextView ... /> to <your.namespace.InstantAutoComplete ... /> 

編輯1

創建一個名爲InstantAutoComplete然後把這個代碼到類新類。

在你的佈局XML使用這個類像

然後找到你的actity此窗口小部件(onCreate方法)。

Look at this example

+0

但問題是我已經在該類中擴展了Activity,並且它是必需的。因此無法像解決方案一樣擴展AutoCompleteTextView。有什麼辦法嗎? – androidcodehunter 2013-03-21 10:40:06

+0

你不需要擴展你的活動,它是新的自定義自動完成textview,只需創建它並在你的佈局中使用。請看編輯答案 – Talha 2013-03-21 10:46:04

+0

https://dl.dropbox.com/u/68130108/AutoComplateTextView。請看這個例子 – Talha 2013-03-21 10:53:47

32

更好的解決方案HERE

你並不需要自定義AutoCompleteTextView。相反,只需撥打autoCompleteTextView.showDropDown()當你需要它.....歡呼:)

+1

不幸的是,這並不總是奏效。我不確定爲什麼,但在某些設備上,當文本字段爲空時,似乎忽略該命令。 – 2014-04-29 18:01:34

+0

您爲它設置了任何閾值嗎? – 2014-04-30 09:40:01

+0

是的,0我甚至事先調用requestFocus – 2014-04-30 12:40:20

20

它爲我工作:

添加到您的對象下一個事件的方法:

myView.setOnFocusChangeListener(new OnFocusChangeListener() { 

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

     } 
    }); 

    myView.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      myView.showDropDown(); 
      return false; 
     } 
    }); 
+1

我不認爲你需要兩個。不觸及視圖也會讓它重點關注? – velocirabbit 2015-10-07 20:30:29

+0

你是對的,我認爲OnTouchListener更好 – 2017-12-04 19:55:49

2

你需要調用requestFocus的( );顯示鍵盤,否則鍵盤不會彈出。

方法強制顯示下拉列表。

autocomptv.setOnTouchListener(new OnTouchListener() { 

     @SuppressLint("ClickableViewAccessibility") 
     @Override 
     public boolean onTouch(View paramView, MotionEvent paramMotionEvent) { 
      // TODO Auto-generated method stub 
      autocomptv.showDropDown(); 
      autocomptv.requestFocus(); 
      return false; 
     } 
    }); 
1

使用本:

text.setOnTouchListener(new View.OnTouchListener(){ 


      @Override 
      public boolean onTouch(View arg0, MotionEvent arg1) { 
       // TODO Auto-generated method stub 
       text.showDropDown(); 
       return false; 
      } 
      }); 
3

這對我的作品完美,這是解決問題的一個簡單的方法:

final ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, usernameLists); 
etUsername.setThreshold(1); 
etUsername.setAdapter(adapter); 
etUsername.setOnTouchListener(new View.OnTouchListener() { 

    @SuppressLint("ClickableViewAccessibility") 
    @Override 
    public boolean onTouch(View paramView, MotionEvent paramMotionEvent) { 
     if (usernameLists.size() > 0) { 
       // show all suggestions 
       if (!etUsername.getText().toString().equals("")) 
        adapter.getFilter().filter(null); 
       etUsername.showDropDown(); 
      } 
     return false; 
    } 
}); 
相關問題