用户从下拉列表中选择项目后禁用 Android AutoCompleteTextView

发布于 2024-12-14 16:56:25 字数 266 浏览 5 评论 0原文

我使用 Android 的 AutoCompleteTextView 和 CursorAdapter 向应用程序添加自动完成功能。在视图的 onItemClickListener() 中(即当用户触摸自动完成的下拉项之一时),我检索文本并将其放置在 EditText 中,以便用户可以在需要时对其进行修改。

但是,当我在 TextView 上调用 setText() 时,会触发自动完成行为并再次显示下拉列表。我只想在用户使用键盘输入新文本时显示下拉列表。有办法做到这一点吗?

I'm using Android's AutoCompleteTextView with a CursorAdapter to add autocomplete to an app. In the view's onItemClickListener() (i.e. when the user touches one of the autocompleted drop down items) I retrieve the text and place it in the EditText so that the user can modify it if they need to.

However, when I call setText() on the TextView the autocomplete behavior is triggered and the dropdown shows again. I'd like to only show the dropdown if the user types new text with the keyboard. Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白云不回头 2024-12-21 16:56:38

不同的方法。
我同意 dismissDropDown() 有效,但就我而言,它没有按预期工作。所以,我用了:

autoCompleteTextView.setDropDownHeight(0);

如果你想再次显示下拉列表,你可以使用

autoCompleteTextView.setDropDownHeight(intValue);

Different approach.
I agreed dismissDropDown() works but in my case, it wasn't working as expected. So, I used:

autoCompleteTextView.setDropDownHeight(0);

And if you want to show the dropdown list again, you an use

autoCompleteTextView.setDropDownHeight(intValue);
怎会甘心 2024-12-21 16:56:37

经过几个小时的黑客攻击后回答我自己的问题:事实证明,您应该实现自己的 OnItemClickListener ,而不是依赖现有的点击侦听器来填充 TextView。我最初实现了 onItemClickListener,因为它使用 Cursor.toString() 的结果来填充文本视图。要更改输出字符串,您应该在 CursorAdapter 中实现 convertToString(Cursor)。返回的 CharSequence 将填充在文本视图中。

这样做还将防止下拉列表再次显示(因为 setText() 会触发完成行为,但默认的 onItemClickListener 不会)。

Answering my own question after a couple hours of hacking at this: It turns out you should implement your own OnItemClickListener and instead rely on the existing click listener to populate the TextView. I had originally implemented the onItemClickListener because it was using the results of Cursor.toString() to populate the text view. To change the output String, you should implement convertToString(Cursor) in your CursorAdapter. The CharSequence that gets returned will be populated in the text view.

Doing this will also prevent the dropdown from showing up again (since setText() triggers the completion behavior but the default onItemClickListener does not).

止于盛夏 2024-12-21 16:56:35

如果您想关闭 AutoCompleteTextView 的下拉列表,您应该使用它的 post(Runnable r) 方法。它对我有用:)

这是一个例子:

mAutoCompleteTextView.post(new Runnable() {
    public void run() {
        mAutoCompleteTextView.dismissDropDown();
    }
}

If you wish to dissmis AutoCompleteTextView's dropdown you should use its post(Runnable r) method. It works for me :)

Here is an example:

mAutoCompleteTextView.post(new Runnable() {
    public void run() {
        mAutoCompleteTextView.dismissDropDown();
    }
}
拥抱影子 2024-12-21 16:56:34

当我们点击 AutoCompleteTextView.onTextChanged() 中建议的项目时,会在 onItemClick 之前执行
因此,为了避免这种情况,请尝试下面的代码。

autocompletetextview.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) {
        if (autocompletetextview.isPerformingCompletion()) {
            // An item has been selected from the list. Ignore.
        } else {
            // Perform your task here... Like calling web service, Reading data from SQLite database, etc...
        }
    }

    @Override
    public void afterTextChanged(final Editable editable) {

    }
});

When we click on item suggested in AutoCompleteTextView.onTextChanged() is performed before onItemClick
So, to avoid this try below code..

autocompletetextview.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) {
        if (autocompletetextview.isPerformingCompletion()) {
            // An item has been selected from the list. Ignore.
        } else {
            // Perform your task here... Like calling web service, Reading data from SQLite database, etc...
        }
    }

    @Override
    public void afterTextChanged(final Editable editable) {

    }
});
恬淡成诗 2024-12-21 16:56:32

您可以使用 dismissDropDown() AutoCompleteTextView 对象的方法。查看文档

You can use the dismissDropDown() method of the AutoCompleteTextView object. Take a look at the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文