java - 按下后退按钮/折叠搜索字段时,Android 搜索菜单项消失

标签 java android xml android-actionbar searchview

我正在使用 android SearchView 在我的操作栏中有一个搜索按钮。我关注了this来自 YouTube 上 Google Developers 的教程,并根据我自己的需要添加了一些调整。但是,每当我退出单击按钮时出现的搜索字段时,搜索按钮就会消失!

这是我的 onCreateOptionsMenu 和 onOptionsItemSelected 函数

onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);


    return true;
}

onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    switch (id) {
        case R.id.action_settings:
            // TODO: settings activity
            break;
        case R.id.action_logout:
            FirebaseAuth.getInstance().signOut();
            break;
        case R.id.action_search:
            // Initialize everything here, because it produces a NullPointerException if initialized in onCreateOptionsMenu
            SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
            SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
            ComponentName componentName = new ComponentName(this, SearchableActivity.class);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));

            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {

                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {

                    return false;
                }
            });
            break;
    }

    return super.onOptionsItemSelected(item);
}

这是我的 menu_main.xml 布局文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.magnus.inline.MainActivity">
<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="Search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView"/>
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />
<item
    android:id="@+id/action_logout"
    android:orderInCategory="200"
    android:title="@string/action_logout"
    app:showAsAction="never" />

和searchable.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="Search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</searchable>

Images

编辑:

我注意到搜索按钮唯一消失的时间是当我在“搜索模式”下按下选项按钮时(参见链接中的最后一张图片)。我有一种感觉,当显示选项菜单时,“action_search”以某种方式转换为选项菜单项,而不是操作栏项(?)。对于任何询问的人,每当我尝试在 onCreateOptionsMenu 函数中调用 searchview MenuItem 的 MenuItemCompat.getActionView() 时,我的应用程序就会崩溃并说它试图在空对象引用上执行它。

这是我的 activity_main.xml 布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.magnus.inline.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AlertDialog.AppCompat.Light"
            android:background="@color/colorPrimaryDark" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

最佳答案

代替

app:showAsAction="ifRoom|collapseActionView"

改为

app:showAsAction="always|collapseActionView

关于java - 按下后退按钮/折叠搜索字段时,Android 搜索菜单项消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45599854/

相关文章:

java - 检查我们是否已连接(WIFI/3-4G) - Android Java

XML 文件作为 Excel 文件

java - 为什么 while 循环不以额外的 "or"条件结束?

java - 在tomcat上部署war文件时出错

java - 在单独的 EJB 方法中开始/结束事务

java - 如何序列化/反序列化 "splashed"复杂类型?

java - 如何根据第一个已知标记分支 SAX 解析器?

java - Facelet 解析或标记 <h :inputText id ="text"/> is parsing 时发生的情况

android - 是否可以将移动设备用作蜂窝天线以提高移动范围?

java - 房间数据库 RxJava 后台线程