ユーザーがクリックするAutoCompleteTextView
と、テキストがなくても提案を表示したいのですが、setThreshold(0)
まったく同じように動作するsetThreshold(1)
ので、ユーザーは提案を表示するために少なくとも1文字を入力する必要があります。
15 に答える
これは文書化された動作です:
threshold
が 0 以下の場合、しきい値 1 が適用されます。
を介して手動でドロップダウンをshowDropDown()
表示できるので、必要なときに表示するように調整できます。または、サブクラス化AutoCompleteTextView
してオーバーライド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
オブジェクトが 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つの答えを適切に組み合わせただけですが、誰かの時間を節約できることを願っています.
アダプターは、最初はフィルタリングを実行しません。
フィルタリングが実行されていない場合、ドロップダウン リストは空です。
そのため、最初にフィルタリングを開始する必要がある場合があります。
これを行うには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();
}
}
});
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;
}
}
それを試してみてください
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;
}
}):