Android - Actionbar Sherlock - Search Filter

一世执手 提交于 2019-11-27 13:43:58

问题


I am trying to implement Action Bar using ActionBar Sherlock. I have three Action Button one of which is a Search Button. On Clicking of the Search Button the Search input field should be displayed which i have already implemented. But i want it to take the full width of the Action Bar. Any idea how i can achieve the same.


回答1:


first make a editTextLayout

layout_search.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/search_edit_text"
    android:cursorVisible="true"
    android:hint="@string/search_friend_hint"
    android:imeOptions="actionDone"
    android:inputType="text"
    android:textColor="@android:color/black"
    android:textCursorDrawable="@android:color/black" />

In you menu xml add android:actionLayout and android:showAsAction="always|collapseActionView" for Search option. For other option make android:showAsAction="ifRoom"

menu.xml

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

    <item
        android:icon="@drawable/ic_action_sort"
        android:orderInCategory="1"
        android:showAsAction="ifRoom"
        android:title="@string/menu_sort"/>
    <item
        android:id="@+id/menu_search"
        android:actionLayout="@layout/layout_search"
        android:icon="@drawable/search"
        android:orderInCategory="0"
        android:showAsAction="always|collapseActionView"
        android:title="@string/search"/>

</menu>

in your activity or fragment override onCreateOptionsMenu like this fragment.java

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu, menu);

        final EditText editText = (EditText) menu.findItem(
                R.id.menu_search).getActionView();
        editText.addTextChangedListener(textWatcher);

        MenuItem menuItem = menu.findItem(R.id.menu_search);
        menuItem.setOnActionExpandListener(new OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // Do something when collapsed
                return true; // Return true to collapse action view
            }

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                editText.clearFocus();
                return true; // Return true to expand action view
            }
        });
    }

and add textWatcherListener

private TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            if (null != mAdapter) {
                mAdapter.getFilter().filter(s);
            }
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    };


来源:https://stackoverflow.com/questions/13560438/android-actionbar-sherlock-search-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!