搜索操作项未显示编辑控件

时间:2015-10-14 17:29:26

标签: android material-design

我有一个与AppCompatActivity相关联的搜索操作项。出现搜索图标,但是当我点击它时,它不显示允许我输入搜索文本的编辑控件。我已经尝试了所有明显的问题,但我似乎无法让这个工作。

此应用加载PDF,根据用户选择切换工具栏。第一个工具栏显示搜索项。选择此选项后,它会添加searchNext和searchPrev按钮。所有这些都有效,直到我切换到Material Design。我认为问题与替换菜单有关,但我不确定。

这是清单:

<activity
    android:name="com.mupdf.MuPDFActivity"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    android:configChanges="orientation|screenSize|keyboardHidden"
    android:label="@string/app_name" >
</activity>

以下是菜单资源:

主菜单:      

<item android:id="@+id/pdf_action_search"
    android:title="@string/pdf_action_search"
    android:icon="@drawable/ic_pdf_action_search"
    android:orderInCategory="100"
    app:showAsAction="ifRoom"/>

<item android:id="@+id/pdf_action_export"
      android:title="@string/pdf_action_export"
      android:icon="@drawable/ic_pdf_action_export"
      android:orderInCategory="100"
      app:showAsAction="ifRoom">

    <menu>
        <item
            android:id="@+id/pdf_action_share"
            android:title="@string/pdf_action_share"
            app:showAsAction="ifRoom"
            app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
        <item
            android:id="@+id/pdf_action_print"
            android:title="@string/pdf_action_print"
            app:showAsAction="always|withText"/>
    </menu>

</item>
<item android:id="@+id/pdf_action_tools"
      android:title="@string/pdf_action_tools"
      android:icon="@drawable/ic_pdf_action_tools"
      android:orderInCategory="100"
      app:showAsAction="ifRoom">

    <menu>
        <item
            android:id="@+id/pdf_action_highlight"
            android:title="@string/pdf_action_highlight"
            app:showAsAction="always|withText"/>
        <item
            android:id="@+id/pdf_action_underline"
            android:title="@string/pdf_action_underline"
            app:showAsAction="always|withText"/>
        <item
            android:id="@+id/pdf_action_strikeout"
            android:title="@string/pdf_action_strikeout"
            app:showAsAction="always|withText"/>
        <item
            android:id="@+id/pdf_action_pen"
            android:title="@string/pdf_action_pen"
            app:showAsAction="always|withText"/>
    </menu>

</item>

替换菜单:

 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="@+id/group_search_mode">
    <item
        android:id="@+id/pdf_menu_search_item"
        android:title="@string/search"
        android:icon="@drawable/ic_pdf_action_search"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
    <item
        android:id="@+id/pdf_menu_search_prev"
        android:title="@string/search_prev"
        android:icon="@drawable/ic_pdf_action_search_prev"
        app:showAsAction="always" />
    <item
        android:id="@+id/pdf_menu_search_next"
        android:title="@string/search_next"
        android:icon="@drawable/ic_pdf_action_search_next"
        app:showAsAction="always" />
</group>

这是活动:

 import android.support.v4.view.MenuItemCompat;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.view.ActionMode;
 import android.support.v7.widget.SearchView;
 import android.support.v7.widget.ShareActionProvider;
 import android.support.v7.widget.Toolbar;
 import android.text.method.PasswordTransformationMethod;
 import android.view.Menu;
 import android.view.MenuItem;

 public class MuPDFActivity extends AppCompatActivity implements      FilePicker.FilePickerSupport
 {
/* The core rendering instance */
enum TopBarMode {Main, Search, Annot, Delete, More, Accept};
enum AcceptMode {Highlight, Underline, StrikeOut, Ink, CopyText};

private TopBarMode   mTopBarMode = TopBarMode.Main;
private AcceptMode   mAcceptMode;
private SearchTask   mSearchTask;

private SearchView mSearchView;
private MenuItem mSearchMenuPrev;
private MenuItem mSearchMenuNext;
private String mSearchString;
private ActionBar mActionBar;
private Toolbar toolbar;

// ***************************************************************************************************
//
//  onCreate
//
// ***************************************************************************************************
@Override
public void onCreate(Bundle savedInstanceState)
{
    /** Called when the activity is first created. */
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR | Window.FEATURE_ACTION_BAR_OVERLAY);
    super.onCreate(savedInstanceState);

    createUI(savedInstanceState);
}

// ***************************************************************************************************
//
//  setupToolbar
//
// ***************************************************************************************************
private void setupToolbar()
{
    //toolbar = (Toolbar) findViewById(R.id.toolbar);
    if(toolbar != null)
    {
        setSupportActionBar(toolbar);
        mActionBar = getSupportActionBar();
        if(mActionBar != null)
        {
            mActionBar.setDisplayHomeAsUpEnabled(true);
            mActionBar.setHomeButtonEnabled(true);
            mActionBar.setDisplayShowHomeEnabled(false);
            mActionBar.setDisplayUseLogoEnabled(false);
            mActionBar.setIcon(null);
            mActionBar.setBackgroundDrawable(new ColorDrawable(MINAppConfiguration.getSharedInstance().getCurrentVisualElements().tbTopBackgroundColor));

            mActionBar.setTitle(pdfName);
        }
    }
    if (Build.VERSION.SDK_INT >= 21)
    {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(MINAppConfiguration.getSharedInstance().getCurrentVisualElements().tbStatusBarBackgroundColor);
    }
}

// ***************************************************************************************************
//
//  onCreateOptionsMenu
//
// ***************************************************************************************************
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.pdf_menu_main, menu);
    MenuItem shareMenuItem = menu.findItem(R.id.pdf_action_share);
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
    setShareIntent();
    return true;
}

// ***************************************************************************************************
//
//  onOptionsItemSelected
//
// ***************************************************************************************************
@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.
    boolean bHandledEvent = false;
    int id = item.getItemId();
    switch (id)
    {
        case R.id.pdf_action_search:
            bHandledEvent = true;
            mActionModeSearch = this.startSupportActionMode(mActionModeSearchCallback);
            break;
    }

    if(!bHandledEvent)
    {
        return super.onOptionsItemSelected(item);
    }
    else
    {
        return bHandledEvent;
    }
}


// ***************************************************************************************************
//
//  mActionModeEditCallback
//
// ***************************************************************************************************
protected ActionMode mActionModeSearch;
private ActionMode.Callback mActionModeSearchCallback = new ActionMode.Callback()
{
    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
    {
        actionMode.getMenuInflater().inflate(R.menu.pdf_menu_search, menu);
        mSearchMenuPrev = (MenuItem) menu.findItem(R.id.pdf_menu_search_prev);
        mSearchMenuPrev.setEnabled(false);
        mSearchMenuNext = (MenuItem) menu.findItem(R.id.pdf_menu_search_next);
        mSearchMenuNext.setEnabled(false);
        //mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.pdf_menu_search_item));

        // TODO this search functionality still isn't working
        MenuItem searchItem = menu.findItem(R.id.pdf_menu_search_item);


        mSearchView=(SearchView)MenuItemCompat.getActionView(searchItem);
        if(mSearchView != null)
        {
            mSearchView.setIconifiedByDefault(false);
            mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
            {
                @Override
                public boolean onQueryTextSubmit(String s)
                {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String s)
                {
                    mSearchString = s;
                    if(s.length() > 0)
                    {
                        mSearchMenuPrev.setEnabled(true);
                        mSearchMenuNext.setEnabled(true);
                    }

                    // Remove any previous search results
                    if (SearchTaskResult.get() != null && !mSearchString.equals(SearchTaskResult.get().txt))
                    {
                        SearchTaskResult.set(null);
                        mDocView.resetupChildren();
                    }

                    return false;
                }
            });
            searchModeOn();
        }
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
    {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
    {
        switch(menuItem.getItemId())
        {
            case R.id.pdf_menu_search_prev:
                mSearchView.clearFocus();
                hideKeyboard();
                search(-1);
                return true;
            case R.id.pdf_menu_search_next:
                mSearchView.clearFocus();
                hideKeyboard();
                search(1);
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode)
    {
        searchModeOff();
        bPopoverLoaded = false;
        mActionModeEdit = null;
        mActionModeSearch = null;
        resetHideToolbarsTimer();
    }
};

// ***************************************************************************************************
//
//  createUI
//
// ***************************************************************************************************
public void createUI(Bundle savedInstanceState)
{
    if (core == null)
        return;

    // Now create the UI.
    // First create the document view
    mDocView = new MuPDFReaderView(this) {
        @Override
        protected void onMoveToChild(int i) {
            if (core == null)
                return;
            mPageNumberView.setText(String.format("%d / %d", i + 1,
                    core.countPages()));
            mPageSlider.setMax((core.countPages() - 1) * mPageSliderRes);
            mPageSlider.setProgress(i * mPageSliderRes);
            super.onMoveToChild(i);
        }

        @Override
        protected void onTapMainDocArea()
        {
            // Hide/Show action bar
            if(isToolbarsVisible())
            {
                setToolbarsVisible(false);
            }
            else
            {
                setToolbarsVisible(true);
            }
        }

        @Override
        protected void onDocMotion() {
            hideButtons();
        }

        @Override
        protected void onHit(Hit item) {
            switch (mTopBarMode) {
            case Annot:
                if (item == Hit.Annotation) {
                    showButtons();
                    mTopBarMode = TopBarMode.Delete;
                    //mTopBarSwitcher.setDisplayedChild(mTopBarMode.ordinal());
                }
                break;
            case Delete:
                mTopBarMode = TopBarMode.Annot;
            default:
                // Not in annotation editing mode, but the pageview will
                // still select and highlight hit annotations, so
                // deselect just in case.
                MuPDFView pageView = (MuPDFView) mDocView.getDisplayedView();
                if (pageView != null)
                    pageView.deselectAnnotation();
                break;
            }
        }
    };
    mDocView.setAdapter(new MuPDFPageAdapter(this, this, core));

    mSearchTask = new SearchTask(this, core)
    {
        @Override
        protected void onTextFound(SearchTaskResult result)
        {
            SearchTaskResult.set(result);
            // Ask the ReaderView to move to the resulting page
            mDocView.setDisplayedViewIndex(result.pageNumber);
            // Make the ReaderView act on the change to SearchTaskResult
            // via overridden onChildSetup method.
            mDocView.resetupChildren();
        }
    };

    // Make the buttons overlay, and store all its
    // controls in variables
    makeButtonsView();

    // Set up the page slider
    int smax = Math.max(core.countPages()-1,1);
    mPageSliderRes = ((10 + smax - 1)/smax) * 2;

    // Activate the seekbar
    mPageSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
    {
        public void onStopTrackingTouch(SeekBar seekBar)
        {
            mDocView.setDisplayedViewIndex((seekBar.getProgress()+mPageSliderRes/2)/mPageSliderRes);
        }

        public void onStartTrackingTouch(SeekBar seekBar)
        {
        }

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {
            updatePageNumView((progress+mPageSliderRes/2)/mPageSliderRes);
        }
    });

    if (savedInstanceState == null || !savedInstanceState.getBoolean("ButtonsHidden", false))
        showButtons();

    // Stick the document view and the buttons overlay into a parent view
    RelativeLayout layout = new RelativeLayout(this);
    layout.addView(mDocView);
    layout.addView(mButtonsView);
    toolbar = new Toolbar(this);
    //toolbar.setMinimumWidth(android.app.ActionBar.LayoutParams.MATCH_PARENT);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    toolbar.setLayoutParams(layoutParams);
    layout.addView(toolbar);
    //RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
    //params.width = android.app.ActionBar.LayoutParams.MATCH_PARENT;

    setContentView(layout);

    // Setup New Toolbar implementation
    setupToolbar();

    // Set up custom colors, etc...
    setCustomAttributes();
}

// ***************************************************************************************************
//
//  onResume
//
// ***************************************************************************************************
@Override
public void onResume()
{
    super.onResume();
    // Start timer to hide toolbars.
    bPopoverLoaded = false;
    resetHideToolbarsTimer();
}

private void showButtons()
{
    if (core == null)
        return;
    if (!mButtonsVisible)
    {
        mButtonsVisible = true;
        // Update page number text and slider
        int index = mDocView.getDisplayedViewIndex();
        updatePageNumView(index);
        mPageSlider.setMax((core.countPages()-1)*mPageSliderRes);
        mPageSlider.setProgress(index*mPageSliderRes);
        if (mTopBarMode == TopBarMode.Search)
        {
            //mSearchText.requestFocus();
            showKeyboard();
        }

        Animation anim = new TranslateAnimation(0, 0, mPageSlider.getHeight(), 0);
        anim.setDuration(200);
        anim.setAnimationListener(new Animation.AnimationListener()
        {
            public void onAnimationStart(Animation animation) {
                mPageSlider.setVisibility(View.VISIBLE);
            }
            public void onAnimationRepeat(Animation animation) {}
            public void onAnimationEnd(Animation animation) {
                mPageNumberView.setVisibility(View.VISIBLE);
            }
        });
        mPageSlider.startAnimation(anim);
    }
}

private void hideButtons()
{
    if (mButtonsVisible)
    {
        mButtonsVisible = false;
        hideKeyboard();
        Animation anim = new TranslateAnimation(0, 0, 0, mPageSlider.getHeight());
        anim.setDuration(200);
        anim.setAnimationListener(new Animation.AnimationListener()
        {
            public void onAnimationStart(Animation animation)
            {
                mPageNumberView.setVisibility(View.INVISIBLE);
            }
            public void onAnimationRepeat(Animation animation) {}
            public void onAnimationEnd(Animation animation)
            {
                mPageSlider.setVisibility(View.INVISIBLE);
            }
        });
        mPageSlider.startAnimation(anim);
    }
}

private void searchModeOn()
{
    if (mTopBarMode != TopBarMode.Search)
    {
        mTopBarMode = TopBarMode.Search;
        //Focus on EditTextWidget
        showKeyboard();
    }
}

private void searchModeOff()
{
    if (mTopBarMode == TopBarMode.Search)
    {
        mTopBarMode = TopBarMode.Main;
        hideKeyboard();
        SearchTaskResult.set(null);
        // Make the ReaderView act on the change to mSearchTaskResult
        // via overridden onChildSetup method.
        mDocView.resetupChildren();
    }
}

private void makeButtonsView()
{
    mButtonsView = getLayoutInflater().inflate(R.layout.buttons,null);

    mPageNumberView = (TextView)mButtonsView.findViewById(R.id.pageNumber);
    mPageNumberView.setVisibility(View.INVISIBLE);
    mInfoView = (TextView)mButtonsView.findViewById(R.id.info);
    mInfoView.setVisibility(View.INVISIBLE);
    mPageSlider = (SeekBar)mButtonsView.findViewById(R.id.pageSlider);
    mPageSlider.setVisibility(View.INVISIBLE);
}

private void search(int direction)
{
    hideKeyboard();
    int displayPage = mDocView.getDisplayedViewIndex();
    SearchTaskResult r = SearchTaskResult.get();
    int searchPage = r != null ? r.pageNumber : -1;
    mSearchTask.go(mSearchString, direction, displayPage, searchPage);
}

@Override
public boolean onSearchRequested()
{
    if (mButtonsVisible && mTopBarMode == TopBarMode.Search)
    {
        hideButtons();
    }
    else
    {
        showButtons();
        searchModeOn();
    }
    return super.onSearchRequested();
}

@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
    if (mButtonsVisible && mTopBarMode != TopBarMode.Search) {
        hideButtons();
    } else {
        showButtons();
        searchModeOff();
    }
    return super.onPrepareOptionsMenu(menu);
}

 }

修改

一个注意事项:搜索项上的get get getSectionView返回null。我认为这可能与问题有关。然后我试着强迫它,但我也不确定是对的。以下是有问题的代码:

    protected ActionMode mActionModeSearch;
private ActionMode.Callback mActionModeSearchCallback = new ActionMode.Callback()
{
    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
    {

        actionMode.getMenuInflater().inflate(R.menu.pdf_menu_search, menu);
        mSearchMenuPrev = (MenuItem) menu.findItem(R.id.pdf_menu_search_prev);
        mSearchMenuPrev.setEnabled(false);
        mSearchMenuNext = (MenuItem) menu.findItem(R.id.pdf_menu_search_next);
        mSearchMenuNext.setEnabled(false);
        mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.pdf_menu_search_item));

        // TODO this search functionality still isn't working
        MenuItem searchItem = menu.findItem(R.id.pdf_menu_search_item);


        mSearchView=(SearchView)MenuItemCompat.getActionView(searchItem);  // ALWAYS COMES BACK AS NULL
        if(mSearchView==null)
        {
            //MenuItemCompat.setShowAsAction(searchItem,MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW|MenuItem.SHOW_AS_ACTION_ALWAYS);
            MenuItemCompat.setShowAsAction(searchItem, MenuItem.SHOW_AS_ACTION_ALWAYS);
            mSearchView = new SearchView(MuPDFActivity.this);
            MenuItemCompat.setActionView(searchItem, mSearchView);
        }
             if(mSearchView != null)
        {
            mSearchView.setIconifiedByDefault(false);
            mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
            {
                @Override
                public boolean onQueryTextSubmit(String s)
                {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String s)
                {
                    mSearchString = s;
                    if(s.length() > 0)
                    {
                        mSearchMenuPrev.setEnabled(true);
                        mSearchMenuNext.setEnabled(true);
                    }

                    // Remove any previous search results
                    if (SearchTaskResult.get() != null && !mSearchString.equals(SearchTaskResult.get().txt))
                    {
                        SearchTaskResult.set(null);
                        mDocView.resetupChildren();
                    }

                    return false;
                }
            });
            searchModeOn();
        }
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
    {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
    {
        switch(menuItem.getItemId())
        {
            case R.id.pdf_menu_search_prev:
                mSearchView.clearFocus();
                hideKeyboard();
                search(-1);
                return true;
            case R.id.pdf_menu_search_next:
                mSearchView.clearFocus();
                hideKeyboard();
                search(1);
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode)
    {
        searchModeOff();
        bPopoverLoaded = false;
        mActionModeEdit = null;
        mActionModeSearch = null;
        resetHideToolbarsTimer();
    }
};

编辑2:

我得到了控制权,但现在“下一个”和“上一个”菜单项已经消失。此外,控件未对齐。而且,我仍然需要扩展搜索控制。

截图:

enter image description here

以下是让我来到这里的更新代码:

         public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
    {
        actionMode.getMenuInflater().inflate(R.menu.pdf_menu_search, menu);
        mSearchMenuPrev = menu.findItem(R.id.pdf_menu_search_prev);
        mSearchMenuPrev.setEnabled(false);
        mSearchMenuNext = menu.findItem(R.id.pdf_menu_search_next);
        mSearchMenuNext.setEnabled(false);
        MenuItem searchItem = menu.findItem(R.id.pdf_menu_search_item);
        MenuItemCompat.setShowAsAction(searchItem, MenuItem.SHOW_AS_ACTION_ALWAYS);
        mSearchView = new SearchView(MuPDFActivity.this);
        MenuItemCompat.setActionView(searchItem, mSearchView);
        //mSearchView.setIconifiedByDefault(false);
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
        {
            @Override
            public boolean onQueryTextSubmit(String s)
            {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s)
            {
                mSearchString = s;
                if(s.length() > 0)
                {
                    mSearchMenuPrev.setEnabled(true);
                    mSearchMenuNext.setEnabled(true);
                }

                // Remove any previous search results
                if (SearchTaskResult.get() != null && !mSearchString.equals(SearchTaskResult.get().txt))
                {
                    SearchTaskResult.set(null);
                    mDocView.resetupChildren();
                }

                return false;
            }
        });
        searchModeOn();
        return true;
    }

如果我调用mSearchView.setIconified(false),我会得到以下截图。菜单项都在那里,但它们都在溢出。如果我扩展溢出,我会看到搜索菜单但是当我点击它时,它会回到我原来的无编辑框输入搜索字符串的问题。换句话说,当我点击“搜索”时,它看起来像下面的第一个屏幕截图:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:3)

我最后不得不手动构建它。我将菜单xml文件更改为:

 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
    <group android:id="@+id/group_search_mode">
        <item
        android:id="@+id/pdf_menu_search_item"
        android:title="@string/search"
        android:icon="@drawable/ic_pdf_action_search"
        app:showAsAction="ifRoom"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
</group>

然后我更改了ActionMode.Callback中的代码以手动构建菜单并强制打开搜索控件。我认为它可能有点矫枉过正但它确实有效:

private static final int PREV_MENU_ITEM_ID = 1;
private static final int NEXT_MENU_ITEM_ID = 2;
protected ActionMode mActionModeSearch;
private ActionMode.Callback mActionModeSearchCallback = new ActionMode.Callback()
{
    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
    {
        actionMode.getMenuInflater().inflate(R.menu.pdf_menu_search, menu);
        // Load search action
        MenuItem searchItem = menu.findItem(R.id.pdf_menu_search_item);
        MenuItemCompat.setShowAsAction(searchItem, MenuItem.SHOW_AS_ACTION_ALWAYS);
        mSearchView = new SearchView(MuPDFActivity.this);
        MenuItemCompat.setActionView(searchItem, mSearchView);
        MenuItemCompat.expandActionView(searchItem);
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setIconified(false);

        // Load prev and next menu items
        mSearchMenuPrev = menu.add(0, PREV_MENU_ITEM_ID, 1, R.string.search_prev);
        mSearchMenuPrev.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mSearchMenuPrev.setEnabled(false);
        mSearchMenuNext = menu.add(0, NEXT_MENU_ITEM_ID, 1, R.string.search_next);
        mSearchMenuNext.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mSearchMenuNext.setEnabled(false);

        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
        {
            @Override
            public boolean onQueryTextSubmit(String s)
            {
                search(1);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s)
            {
                mSearchString = s;
                if(s.length() > 0)
                {
                    mSearchMenuPrev.setEnabled(true);
                    mSearchMenuNext.setEnabled(true);
                }

                // Remove any previous search results
                if (SearchTaskResult.get() != null && !mSearchString.equals(SearchTaskResult.get().txt))
                {
                    SearchTaskResult.set(null);
                    mDocView.resetupChildren();
                }

                return false;
            }
        });
        searchModeOn();
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
    {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
    {
        switch(menuItem.getItemId())
        {
            case PREV_MENU_ITEM_ID:
                mSearchView.clearFocus();
                hideKeyboard();
                search(-1);
                return true;
            case NEXT_MENU_ITEM_ID:
                mSearchView.clearFocus();
                hideKeyboard();
                search(1);
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode)
    {
        searchModeOff();
        bPopoverLoaded = false;
        mActionModeEdit = null;
        mActionModeSearch = null;
        resetHideToolbarsTimer();
    }
};

我希望这可以帮助那些人。太痛苦了!