简体   繁体   English

如何在上下文操作栏中创建“搜索”字段?

[英]How do I create a “Search” field in a contextual action bar?

I have an activity with an ActionBar. 我有一个ActionBar的活动。 One of the options is to search a document (not necessarily local). 其中一个选项是搜索文档(不一定是本地文档)。 I want to use a contextual action bar (CAB) with the following items: 我想使用具有以下项目的上下文操作栏(CAB):

  • edit field - place for user to enter text they want to search for 编辑字段 - 用户输入要搜索的文本的位置
  • Previous Button - moves to the previous item that matches the search 上一个按钮 - 移动到与搜索匹配的上一个项目
  • Next Button - moves to the next item that matches the search 下一个按钮 - 移动到与搜索匹配的下一个项目

I want to use a CAB for several reasons. 我想使用CAB有几个原因。 Primarily, I want the user to pick an option which will bring up the CAB defined above. 首先,我希望用户选择一个选项,该选项将调出上面定义的CAB。 When the user is done with the search, they select the done button and the ActionBar returns to previous state. 当用户完成搜索时,他们选择完成按钮,ActionBar返回到先前状态。

Here's the problem. 这是问题所在。 I can't get the search item to appear in my CAB in an expanded state. 我无法让搜索项在扩展状态下显示在我的CAB中。 NOTE: The activity I'm attempting to modify is NOT the main activity but is an activity launched by the user based on certain events. 注意:我尝试修改的活动不是主要活动,而是用户根据特定事件启动的活动。 Once this activity is loaded, the user may or may not decide to use the 'search' functionality I'm describing. 加载此活动后,用户可能会也可能不会决定使用我正在描述的“搜索”功能。 I also do not want Android to do the searching. 我也不希望Android进行搜索。 I have an API that will search for the results I want and allow me to navigate to the prev/next search result. 我有一个API,可以搜索我想要的结果,并允许我导航到上一个/下一个搜索结果。

My code for the CAB (which is being called correctly) is: 我的CAB代码(正确调用)是:

     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);
        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.pdf_menu_search_item).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
        {
            @Override
            public boolean onQueryTextSubmit(String s)
            {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s)
            {
                return false;
            }
        });
        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:
                findPrevSearchResult();
                return true;
            case R.id.pdf_menu_search_next:
                findNextSearchResult();
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode)
    {
        //resetHideToolbarsTimer();
    }
};

The CAB menu code is: CAB菜单代码是:

 <?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="collapseActionView|ifRoom"
        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="ifRoom" />
    <item
        android:id="@+id/pdf_menu_search_next"
        android:title="@string/search_next"
        android:icon="@drawable/ic_pdf_action_search_next"
        app:showAsAction="ifRoom" />
</group>

I also changed my activity definition in AndroidManifest.xml although I'm not sure this is required: 我还在AndroidManifest.xml中更改了我的活动定义,虽然我不确定这是否是必需的:

         <activity
        android:name="com.myapp.myActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data
            android:name="android.app.default_searchable"
            android:value="com.myapp.myActivity" />
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

When I run the app, the activity load correctly. 当我运行应用程序时,活动正确加载。 When the user selects the search option, I load the CAB and it seems to be running fine. 当用户选择搜索选项时,我加载CAB,它似乎运行正常。 I've stepped through the code to make sure the onCreateActionMode is functioning as expected. 我已逐步完成代码以确保onCreateActionMode按预期运行。 But, all I see is the icon for search, next and previous, with the 'Done' button. 但是,我所看到的只是搜索的图标,下一个和上一个,使用“完成”按钮。 When I touch the 'Search' icon, it doesn't do anything. 当我触摸“搜索”图标时,它什么也没做。 No text field is created in the CAB for me to enter text. 在CAB中没有创建文本字段供我输入文本。 What am I doing wrong???? 我究竟做错了什么????

Screenshot showing the CAB coming up. 显示CAB即将到来的屏幕截图。 As you can see, the search icon shows but doesn't do anything when I press it. 如您所见,搜索图标显示但在我按下时没有做任何事情。 What I really need is for the search edit box to appear by default. 我真正需要的是默认情况下显示搜索编辑框。

在此输入图像描述

First, we should set always value for app:showAsAction of all menu items: 首先,我们应该设置always为价值app:showAsAction所有菜单项:

<?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:icon="@drawable/ic_pdf_action_search"
            android:title="@string/search"
            app:actionViewClass="android.support.v7.widget.SearchView"
            app:showAsAction="always"/>
        <item
            android:id="@+id/pdf_menu_search_prev"
            android:icon="@drawable/ic_pdf_action_search_prev"
            android:title="@string/search_prev"
            app:showAsAction="always"/>
        <item
            android:id="@+id/pdf_menu_search_next"
            android:icon="@drawable/ic_pdf_action_search_next"
            android:title="@string/search_next"
            app:showAsAction="always"/>
    </group>
</menu>

Secondary, in this case we don't need to set intent filter for our Activity and searchable info for our SearchView . 辅助,在这种情况下,我们不需要为我们的SearchView设置Activity和可搜索信息的intent过滤器。

Definition of this Activity in AndroidManifest.xml : AndroidManifest.xml中此活动的定义:

<activity
    android:name="com.myapp.myActivity"
    android:label="@string/app_name" />

ActionMode.Callback implementation: ActionMode.Callback实现:

private ActionMode.Callback mActionModeSearchCallback = new ActionMode.Callback() {

    private SearchView mSearchView;

    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        actionMode.getMenuInflater().inflate(R.menu.home, menu);
        mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.pdf_menu_search_item));
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
        mSearchView.requestFocus();
        return true;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.pdf_menu_search_prev:
                findPrevSearchResult();
                return true;
            case R.id.pdf_menu_search_next:
                findNextSearchResult();
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode) {

    }
};

I just tried this code on three 4.0+ devices and it was fully working. 我只是在三个4.0+设备上尝试了这个代码,它完全正常工作。 But I didn't test on devises with lower OS versions. 但我没有测试较低操作系统版本的设备。

Hope it will be helpful for you. 希望它对你有所帮助。

i have problem searchView in CAB so i set layout(with edittext) in manu item, it give me same view as searchview try this i hope it works for you 我在CAB中有问题searchView所以我在manu项目中设置布局(使用edittext),它给我与searchview相同的视图尝试这个我希望它适合你

menu/search.xml 菜单/ search.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/edt_mySearch"
        android:actionLayout="@layout/search_bar"
        android:enabled="true"
        android:showAsAction="always"
        android:visible="true"/>

</menu>

layout/search _bar.xml layout / search _bar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


    <EditText 
        android:layout_alignParentLeft="true"
        android:id="@+id/edt_search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint=" Search..."
        />


</RelativeLayout>

and myAction mode 和myAction模式

mActionModeCallback = new ActionMode.Callback() {

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                // mode.setTitle("Demo");
                getSupportMenuInflater().inflate(R.menu.search, menu);
                RelativeLayout m = (RelativeLayout) menu.findItem(
                        R.id.edt_mySearch).getActionView();
                EditText mSearchView = (EditText) m
                        .findViewById(R.id.edt_search);

                mSearchView.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start,
                            int before, int count) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start,
                            int count, int after) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        // search here
                    }
                });
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
            }

        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除上下文操作栏(CAB)中的默认项目? - How do I delete a default item in the contextual action bar (CAB)? 如何从列表适配器显示上下文操作栏? - How Do i Show Contextual Action Bar from List Adapter? 如何使用上下文操作栏来处理汉堡包图标和插入符号图标? - How do I handle the hamburger-icon and up-caret-icon with a contextual action bar? 在上下文操作栏中,如何通过使选项卡不可单击/可滑动来禁用滑动选项卡? - How do I disable sliding tabs when in contextual action bar, by making the tabs not clickable/swipeable? 如何为ListFragment正确启动多选上下文操作栏(有问题) - How do I properly initiate a multi-select Contextual Action Bar for ListFragment (having issues) 如何更改上下文操作栏的文本颜色? Android的 - How do you change the text color of the contextual Action Bar? Android 如何在Android支持库中使用上下文操作栏? - How can I use a contextual action bar with the Android support library? 如何更改上下文操作栏和下划线的背景颜色? - How can i change the background color of contextual action bar and underline? 当我单击上下文操作栏上的项目时如何插入菜单 - How to insert a menu when i click in a item on the contextual action bar 如何更改上下文操作栏的主题 - How to change the theme of contextual action bar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM