簡體   English   中英

Android - 調用 setText 后 AutoCompleteTextView 不顯示

[英]Android - AutoCompleteTextView not showing after setText is called

我在使用AutoCompleteTextView時遇到了一個奇怪的問題。

我有一個AutoCompleteTextView ,在輸入時會顯示城市的建議。 通過JSON從遠程服務器檢索城市列表。 當我使用軟鍵盤或軟鍵盤上的Mic按鈕時,建議工作正常。 AutoCompleteTextView會顯示建議的城市。

但是,當我嘗試使用myAutoCompleteTextView.setText("Chi")設置文本時遇到問題,自動完成不顯示..我也嘗試過myAutoCompleteTextView.append("Chi")但仍然沒有運氣..

適配器在那里,只是建議沒有顯示。

有小費嗎?

謝謝。

是的,你是對的, AutocompleteTextview有一個錯誤,用於顯示使用setText("");默認建議setText(""); 方法。

但您可以通過添加更多代碼行來實現此目的,如下所示。

autoText.postDelayed(new Runnable() {
            @Override
            public void run() {
                autoText.showDropDown();
            }
        },500);
        autoText.setText("chi");
        autoText.setSelection(autoText.getText().length());

Biraj Zalavadia的回答工作,但您必須在Runnable中寫入“settext”。 像這樣:

 mACTextViewEmail.postDelayed(new Runnable() {
            @Override
            public void run() {
                mACTextViewEmail.showDropDown();    
                mACTextViewEmail.setText("My text is here");
                mACTextViewEmail.setSelection(mACTextViewEmail.getText().length());
            }
        },500);

這是由於過濾 ,無需任何額外的代碼來管理它,我發現它非常容易和工作。

Google Dev。 參考鏈接

autoText.setText("Default Value here",false)
autoText.setSelection(autoText.text.count()) // kotlin

根據文檔第二個參數,您可以傳遞進行過濾。

boolean:如果為false,則不會因此調用而執行過濾。

我搜索了一下,發現這個解決方案效果很好看看這個問題

fun AutoCompleteTextView.showDropdown(adapter: ArrayAdapter<String>?) {
  if(!TextUtils.isEmpty(this.text.toString())){
      adapter?.filter?.filter(null)
  }
}

在 kotlin 語言中,您可以使用此分機 function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM