Android:在 SearchView 中输入文本时防止弹出文本字段

2024-02-01

你好 Android 开发者,

我的 Android 系统有问题搜索视图 http://developer.android.com/reference/android/widget/SearchView.html小部件。我想做的是将“实时”文本过滤器附加到我的 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 中获利。

任何建议表示赞赏。 此致

Felix


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

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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:在 SearchView 中输入文本时防止弹出文本字段 的相关文章

随机推荐