gpt4 book ai didi

android - SearchView 不调用 onOptionsItemSelected

转载 作者:太空狗 更新时间:2023-10-29 15:32:32 26 4
gpt4 key购买 nike

“错误”好奇怪其他时候我已经这样做了,但从来没有解决过这个问题......

如果我在菜单上放置“appname:actionViewClass="android.support.v7.widget.SearchView",我的搜索 View 不会调用 onOptionsItemSelected。

list

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<activity
android:name=".DashBoardActivity"
android:label="">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

dash_board.xml(菜单)(如果我撤回 actionViewClass 他调用(onOptionsItemSelected),但如果我放他不调用)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appname="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.servic.DashBoardActivity" >


<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
appname:showAsAction="always"
appname:actionViewClass="android.support.v7.widget.SearchView"/>

<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
appname:showAsAction="always"/>



</menu>

仪表板 Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dash_board, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}

@Override
public boolean onOptionsItemSelected(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_search:
mSearchView
.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
ToastMensagem("Digite um endereço.");
} else {
BuscarEndereco(mSearchView.getQuery().toString());
}
}



});
break;
}


return super.onOptionsItemSelected(item);
}

错误是,没有调用 onOptionsItemSelected,我需要操作 searchview

最佳答案

我知道现在为时已晚,但如果以后有人发现它有帮助,我仍在写答案。昨天我遇到了同样的问题,我通过更改菜单 xml 文件中的以下行解决了这个问题

appname:showAsAction="always"

appname:showAsAction="always|collapseActionView"

您必须在 appname:showAsAction 中添加 collapseActionView 以及 always 以在 searchView 上启用 onOptionsItemSelected

关于android - SearchView 不调用 onOptionsItemSelected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26702744/

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