Android : 텍스트가 입력되지 않은 경우 AutoCompleteTextView에 제안 표시 때, – 그러나

내가 사용하고 AutoCompleteTextView사용자가 클릭 할, 나는 그것이 텍스트가없는 경우에도 제안을 표시 할 때, – 그러나 setThreshold(0)정확하게 작동하는 동일 setThreshold(1)– 사용자가 제안을 표시하는 적어도 하나 개의 문자를 입력해야하므로.



답변

이것은 문서화 된 행동입니다 .

경우 threshold미만 또는 0 일, 1 임계 값을 적용한다.

을 통해 드롭 다운을 수동으로 표시 showDropDown()할 수 있으므로 원할 때 표시 할 수 있습니다. 또는 서브 클래스 AutoCompleteTextView및 override 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;
   }
});


답변

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

문제를 해결했습니다.

그것은 올바르게 결합 된 두 가지 대답이지만 누군가 시간을 절약 할 수 있기를 바랍니다.


답변

어댑터는 처음에 필터링을 수행하지 않습니다.
필터링을 수행하지 않으면 드롭 다운 목록이 비어 있습니다.
필터링을 초기에 수행해야 할 수도 있습니다.

이를 위해 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("");
        }
    }
}