SearchView不调用onOptionsItemSelected

时间:2014-11-02 18:22:54

标签: android android-actionbar searchview

"错误"好奇怪其他时候我已经做过了,从来没有尝试过这个问题......

我的searchview不会调用onOptionsItemSelected如果我把" appname:actionViewClass =" android.support.v7.widget.SearchView"在菜单上。

清单

<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>

DashBoardActivity

@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

3 个答案:

答案 0 :(得分:15)

我现在知道它已经太晚了,但如果其他人发现它有用,我仍在写答案。 昨天我遇到了同样的问题,我通过更改菜单xml文件中的以下行来解决它

appname:showAsAction="always"

appname:showAsAction="always|collapseActionView"

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

答案 1 :(得分:0)

每当我将collapseActionView添加到菜单项时,操作栏标题将隐藏在搜索视图上。这就是为什么我在寻找一种方法将其保留为

&#13;
&#13;
android:showAsAction="always"
&#13;
&#13;
&#13;

使用onOptionsItemSelected功能。如果有人发现任何事情,请告诉我! :)

答案 2 :(得分:0)

请注意,要提交搜索结果,您不应使用SearchView.setOnQueryTextFocusChangeListener

应该使用SearchView.setOnQueryTextListener代替。

以下是搜索提交监听器的示例:

        case R.id.action_search:
            mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                @Override
                public boolean onQueryTextSubmit(String query) {
                    Toast.makeText(getActivity(),
                            getString(R.string.abc_searchview_description_submit),
                            Toast.LENGTH_SHORT)
                            .show();
                    updateUI(query);
                    return true;
                }

                @Override
                public boolean onQueryTextChange(String query) {
                    // Clear search
                    if (query.equals("")) {
                        updateUI(null);
                    }
                    return true;
                }
            });