gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:02 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com