【问题标题】:Android: Prevent text field pop-up when entering text in a SearchViewAndroid:在 SearchView 中输入文本时防止弹出文本字段
【发布时间】:2012-05-01 22:47:09
【问题描述】:

Android 开发者您好,

Android SearchView 小部件有问题。我要做的是将“实时”文本过滤器附加到我的 ListView (文本输入自动刷新过滤器结果)。它实际上工作得很好,用这些行让它在我的 ListActivity 上工作并不费力:

private SearchView listFilter;

this.listFilter = (SearchView) findViewById(R.id.listFilter);
this.listFilter.setOnQueryTextListener(this);
this.listFilter.setSubmitButtonEnabled(false);

this.getListView().setOnItemClickListener(this);
this.getListView().setTextFilterEnabled(true);

// from OnQueryTextListener
public boolean onQueryTextChange(String newText) {
    if (newText.isEmpty()) {
        this.getListView().clearTextFilter();
    } else {
        this.getListView().setFilterText(newText);
    }
    return true;
}

这里是 xml 小部件声明

<SearchView
    android:id="@+id/listFilter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:iconifiedByDefault="false"
    android:queryHint="enter text to filter" />

现在我的问题是,每次我在 SearchView 中输入文本时,都会立即弹出一个奇怪的文本字段,显示与我刚刚输入的文本相同的文本,这有点没用,因为我可以在 SearchView 本身中看到我的输入,它部分挡住了我的列表条目的视线,这很烦人。

有什么方法可以防止在 SearchView 中输入时弹出该文本字段?我在 xml 定义的小部件选项和 java 类引用上都找不到任何属性。

我知道还有另一种方法可以通过使用 EditText 和 TextWatcher 来提供过滤器功能,但是我必须自己处理过滤器,并且无法从 SearchView 为我处理它中获利。

感谢任何建议。 最好的问候

菲利克斯

【问题讨论】:

    标签: android listview search android-widget


    【解决方案1】:

    我发现了如何摆脱那个丑陋的弹出窗口。诀窍是直接使用过滤器。下面的代码假设您已经在 customAdapter 中实现了可过滤器。

    public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText)) {
           m_listView.clearTextFilter();
        } else {
           ContactsAdapter ca = (ContactsAdapter)lv.getAdapter();
           ca.getFilter().filter(newText);
           //following line was causing the ugly popup window.
           //m_listView.setFilterText(newText);
        }
       return true;
    }
    

    【讨论】:

    • 感谢您的回复。我已经恢复到 TextWatcher 解决方法,但是一旦我找到了一些时间,我会测试它,如果它有效,会给你一个绿色的勾号。 :)
    • 您能发布您使用 TextWatcher 的解决方案吗?
    猜你喜欢
    • 2020-07-13
    • 2023-04-03
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    相关资源
    最近更新 更多