アンドロイド:AutoCompleteTextView はテキストが入力されていない場合に候補を表示します
-
22-09-2019 - |
質問
使っています AutoCompleteTextView
, ユーザーがクリックしたときに、テキストがなくても候補を表示したいのですが、 setThreshold(0)
とまったく同じように機能します setThreshold(1)
- そのため、ユーザーは候補を表示するには少なくとも 1 文字を入力する必要があります。
解決
これは<文書化された動作です/ A>:
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 のコードは、1 つしかない場合でも非常にうまく機能します。 InstantAutoComplete
物体。 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);
上記Destilの答えはほとんど動作しますが、1つの微妙なバグがあります。ユーザーが最初にそれが動作フィールドにフォーカスを与えたとき、彼らが去ると、フィールドに戻っしかし、もしそれがmPopupCanBeUpdatedの値はまだそれが隠されたときからfalseになりますので、ドロップダウンが表示されません。
:修正はに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("");
}
}
}
あなたがonFocusChangeListener使用することができます。
TCKimlikNo.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
TCKimlikNo.showDropDown();
}
}
});
。 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;
}
}
ジャストタッチでこのメソッドを呼び出すか、あなたが望むautoCompleteTextViewかのイベントをクリックします。
autoCompleteTextView.showDropDown()
それを試してみてください
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;
}
}):
これは私のために働いた、擬似コード:
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@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);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
this.showDropDown();
return super.onTouchEvent(event);
}
}
<時間> ただ、JavaであなたのonCreateメソッドにこれを貼り付ける
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.Loc_names));
textView1 =(AutoCompleteTextView) findViewById(R.id.acT1);
textView1.setAdapter(arrayAdapter);
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
textView1.setMaxLines(5);
textView1.showDropDown();
}
});
そしてこれはあなたのXMLファイルに...
<AutoCompleteTextView
android:layout_width="200dp"
android:layout_height="30dp"
android:hint="@string/select_location"
android:id="@+id/acT1"
android:textAlignment="center"/>
と値下string.xmlをのArrayを作成...
<string-array name="Loc_names">
<item>Pakistan</item>
<item>Germany</item>
<item>Russia/NCR</item>
<item>China</item>
<item>India</item>
<item>Sweden</item>
<item>Australia</item>
</string-array>
そして、あなたが行ってもいいです。
セブン年後、みんな、問題は同じままです。ここでは力が愚かなポップアップは任意の条件で自分自身を表示することの機能を持つクラスです。あなたがする必要があるすべては、あなたのAutoCompleteTextViewにアダプタを設定し、それにいくつかのデータを追加し、コールshowDropdownNow()
機能いつでもすることです。
@デビッドVávraにクレジット。それは、彼のコードに基づいています。
import android.content.Context
import android.util.AttributeSet
import android.widget.AutoCompleteTextView
class InstantAutoCompleteTextView : AutoCompleteTextView {
constructor(context: Context) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
override fun enoughToFilter(): Boolean {
return true
}
fun showDropdownNow() {
if (adapter != null) {
// Remember a current text
val savedText = text
// Set empty text and perform filtering. As the result we restore all items inside of
// a filter's internal item collection.
setText(null, true)
// Set back the saved text and DO NOT perform filtering. As the result of these steps
// we have a text shown in UI, and what is more important we have items not filtered
setText(savedText, false)
// Move cursor to the end of a text
setSelection(text.length)
// Now we can show a dropdown with full list of options not filtered by displayed text
performFiltering(null, 0)
}
}
}