【问题标题】:Weird gray popup with text when typing in SearchView在 SearchView 中键入时带有文本的奇怪灰色弹出窗口
【发布时间】:2015-05-15 07:26:57
【问题描述】:

我的应用中有一个 SearchView,当我输入它时,每个字符都会显示在一个奇怪的弹出窗口中,如下所示。

布局:

<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 字段都没有问题。

【问题讨论】:

  • 这个搜索小部件是放在 ToolBar 还是 ActionBar 中?如果是这样,它可能会显示菜单标题或尝试显示自动完成弹出窗口之类的内容。
  • @Stan 它在片段布局中。我已经用完整的布局代码更新了这个问题。
  • 它看起来像是 AutoCompleteEditText 的提示,我猜它包含在 SearchView 本身中。

标签: android searchview


【解决方案1】:

这与ListView 的过滤功能的内置功能有关。正如 JonasCz 所指出的,如何解决这个问题可以在 here 找到。我的代码更改为onQueryTextChange() 方法来解决这个问题(基本上使用Adapter 的过滤功能):

    @Override
    public boolean onQueryTextChange(String newText) {
        Filter filter = adapter.getFilter();
        if (TextUtils.isEmpty(newText)) {
            filter.filter("");
        } else {
            filter.filter(newText);
        }
        return true;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    相关资源
    最近更新 更多