139

ユーザーがクリックするAutoCompleteTextViewと、テキストがなくても提案を表示したいのですが、setThreshold(0)まったく同じように動作するsetThreshold(1)ので、ユーザーは提案を表示するために少なくとも1文字を入力する必要があります。

4

15 に答える 15

171

これは文書化された動作です:

thresholdが 0 以下の場合、しきい値 1 が適用されます。

を介して手動でドロップダウンをshowDropDown()表示できるので、必要なときに表示するように調整できます。または、サブクラス化AutoCompleteTextViewしてオーバーライドenoughToFilter()し、常に返されますtrue

于 2010-01-24T11:36:53.383 に答える
132

これが私のクラス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 ... />
于 2011-04-25T22:29:56.950 に答える
53

最も簡単な方法:

setOnTouchListener と showDropDown() を使用するだけです

AutoCompleteTextView text;
.....
.....
text.setOnTouchListener(new View.OnTouchListener(){
   @Override
   public boolean onTouch(View v, MotionEvent event){
      text.showDropDown();
      return false;
   }
});
于 2013-01-24T15:05:21.387 に答える
18

Destil のコードは、InstantAutoCompleteオブジェクトが 1 つしかない場合にうまく機能します。ただし、2つでは機能しませんでした-理由はわかりません。しかし、showDropDown()(CommonsWareがアドバイスしたように)次のように入れるとonFocusChanged()

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

それは問題を解決しました。

2つの答えを適切に組み合わせただけですが、誰かの時間を節約できることを願っています.

于 2012-07-04T00:49:54.453 に答える
11

アダプターは、最初はフィルタリングを実行しません。
フィルタリングが実行されていない場合、ドロップダウン リストは空です。
そのため、最初にフィルタリングを開始する必要がある場合があります。

これを行うにはfilter()、エントリの追加が完了した後に呼び出すことができます。

adapter.add("a1");
adapter.add("a2");
adapter.add("a3");
adapter.getFilter().filter(null);
于 2014-09-08T22:24:26.663 に答える
7

onFocusChangeListener を使用できます。

TCKimlikNo.setOnFocusChangeListener(new OnFocusChangeListener() {

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

            }

        }
    });
于 2013-01-22T07:34:33.437 に答える
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; 
    } 
}
于 2012-03-29T12:44:02.890 に答える
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;
            }
        }):
于 2015-11-15T20:01:20.737 に答える