在 SearchView 中键入时,带有文本的奇怪灰色弹出窗口 [英] Weird gray popup with text when typing in SearchView
问题描述
我的应用中有一个 SearchView,当我输入它时,每个字符都显示在一个奇怪的弹出窗口中,如下所示.
I have a SearchView in my app and when I type in it, every character shows up in a weird popup as can be seen below.
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@color/white"
tools:context="com.example.myapp">
<SearchView
android:id="@+id/search_view"
android:queryHint="@string/select_story_query_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="@color/gray"
android:dividerHeight="1dp"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/create_new_story_button"
android:id="@+id/createStoryButton"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:layout_marginBottom="5dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="@drawable/low_priority_button_background"
android:textColor="@color/black" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/done"
android:id="@+id/select_story_done"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:layout_marginTop="5dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="@drawable/button_background"
android:textColor="@color/white" />
</LinearLayout>
代码:
mSearchView = (SearchView) view.findViewById(R.id.search_view);
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(false);
mSearchView.setQueryHint(getString(R.string.query_hint));
听众:
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
mListView.clearTextFilter();
} else {
mListView.setFilterText(newText);
}
return true;
}
我在搜索时似乎找不到有关此问题的任何信息(我猜我没有使用正确的关键字).这也发生在运行不同 Android 版本(N5 5.1,O+O 4.4)的多个设备上,只有这个特定字段有这个问题,所有其他 EditText 字段都没有问题.
I can't seem to find anything about this issue when I search (guess I am not using the correct keywords). This is also happening on multiple devices running different versions of Android (N5 with 5.1, O+O with 4.4) and only this particular field has this issue, all other EditText fields have no issue.
推荐答案
这与 ListView
过滤器功能的内置功能有关.正如 JonasCz 指出的,如何解决这个问题可以在这里找到.我的代码更改为 onQueryTextChange()
方法来解决这个问题(基本上使用 Adapter
的过滤器功能):
This has to do with a built in functionality of ListView
's filter functionality. As JonasCz pointed out, how to solve this can be found here. My code changes to the onQueryTextChange()
method to fix this (basically use the Adapter
's filter functionality):
@Override
public boolean onQueryTextChange(String newText) {
Filter filter = adapter.getFilter();
if (TextUtils.isEmpty(newText)) {
filter.filter("");
} else {
filter.filter(newText);
}
return true;
}
这篇关于在 SearchView 中键入时,带有文本的奇怪灰色弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!