Android的 - 动作条福尔摩斯 - 搜索过滤器福尔摩斯、过滤器、动作、Android

2023-09-12 01:40:00 作者:无灵魂游走

我想用动作条夏洛克执行行动吧。我有三个操作按钮其中之一是搜索按钮。在单击搜索按钮,搜索输入字段应该显示我已经实现了。但我想它要采取的操作栏的宽度。任何想法我如何能达到同样的。

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.

推荐答案

先做个editTextLayout

first make a editTextLayout

layout_search.xml

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

在你的菜单XML添加安卓actionLayout 安卓showAsAction =永远| collapseActionView 对于搜索选项。 对于其他选项化妆安卓showAsAction =ifRoom

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

menu.xml文件

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>

在你的活动或片段覆盖onCreateOptionsMenu这样 fragment.java

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
            }
        });
    }

和添加textWatcherListener

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

        }
    };