为什么 AutoCompleteTextView.showDropDown() 不会随叫随到?

Why won't the AutoCompleteTextView.showDropDown() not trigger on call?

问题: 我的AutoCompleteTextView.**showDropDown()** 在被 onFocusChange 调用时会工作,但在被 onTextChanged 调用时不会工作。在调试期间,当我希望它根据我创建的日志消息执行 showDropDown() 时,onTextChanged 方法被正确调用,但没有任何反应。我注意到之前发生了一个 SpannableStringBuilder 错误,但我记得(可能是错误的)我过去检查过这个错误时,这是​​一个开放票证的常见错误。我不确定是不是这个原因。

我想做的是: ... 是每个问这类问题的人都想做的,得到 AutoCompleteTextView 来显示完整的随时列出 AutoCompleteTextView 聚焦且为空(我的第一项是:“”)

我尝试过的:我在这个论坛上尝试了很多“绿色检查”的解决方案,但是 none 目前除了第一次关注时它们都有效.它们将导致完整的项目列表出现,但它不会在退格回到零时出现。正如大多数建议一样,我正在使用 change listeners 的组合。我只是真的认为这会根据日志记录值起作用,并且会在适当的时间调用它?

我的日志记录语句: 当我希望它显示该方法在我喜欢的时候被调用时写入。

E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
    SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/OnTextChanged: showDropDown() 
    start: 0
    before: 1
    count: 0
    length: 0

我目前的版本和努力:

 ArrayAdapter<String> topicsAdapter = DBQueryTools.captureDBTopics(this);
topic.setAdapter(topicsAdapter);
topic.setOnItemClickListener((parent, view, position, id) -> {
    if(!topic.getText().toString().equals("")) {
        question.setText("");
        rListNotes.setAdapter(null);
        customSearch.setText(null);
        loadNotes(captureNotes(researchDatabase.getNotesDao().getNotesOnTopic(topic.getText().toString())));
    }
});

topic.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(before == 1 && count == 0 && s.length()==0) {
            topic.showDropDown();
            Log.e("OnTextChanged", "showDropDown() " + s + "\n" + "start: " + start + "\nbefore: " + before + "\ncount: " + count + "\nlength: " + s.length());
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});
topic.setOnFocusChangeListener(new View.OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            topic.showDropDown();
            Log.e("HasFocus", "showDropDown");
        }
    }
});

我能够使 InstantAutoComplete 视图的修改版本正常工作(结合 here and here

public class InstantAutoComplete extends AppCompatAutoCompleteTextView {

    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) {
            // This part was necessary to get it working when not
            // inside a TextInputLayout and multiple per activity
            if( !maybeShowSuggestions() ) {
                post(this::maybeShowSuggestions);
            }
        }
    }

    private boolean maybeShowSuggestions() {
        if( getWindowVisibility() == View.VISIBLE ) {
            performFiltering(getText(), 0);
            showDropDown();
            return true;
        }
        else {
            return false;
        }
    }
}

用于插入的 XML 看起来像(在没有 TextInputLayout 的情况下也有效)

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/text_layout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Type words here"
    android:layout_margin="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/focus_stealer" >

    <com.example.autocompletetextviewdemo.InstantAutoComplete
        android:id="@+id/autocomplete_text_view"
        android:layout_width="match_parent"
        android:completionThreshold="0"
        android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

并使用适配器进行设置

    private static final String[] WORDS = new String[] {
        "Aardvark", "Apple", "Baby", "Boron", "Carbon"
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, WORDS);
        AutoCompleteTextView textView = findViewById(R.id.autocomplete_text_view);
        textView.setAdapter(adapter);   
    }

这适用于每个 activity 的多个 InstantAutoComplete 下拉菜单,并且在活动之间来回导航之后。如果不对自定义 class 进行额外更改,它不适用于 activity 中不在 TextInputLayout.

中的多个实例