Android 中的自定义搜索?

Custom search in Android?

我一直致力于在 Android 应用栏中实施 Custom search。因此,我在 menu_main.xml

中添加了以下代码
<item android:id="@+id/action_search"
    android:title="search"
    android:icon="@drawable/ic_search"
    app:showAsAction="ifRoom|collapseActionView"
    android:hint="Enter Tags"
    app:actionViewClass="android.support.v7.widget.SearchView" />

现在我在看HomeActivity。它看起来像这样:

非常好!我还没有实现上面 SearchView 的后端代码。现在,我想在上面添加一些以下功能 Search bar

问题

任何帮助将不胜感激。

编辑-1

我正在添加 onCreateOptionsMenu 方法:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    if (null != searchView) {
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    }

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered
            return true;
        }

        public boolean onQueryTextSubmit(String query) {
            //Here u can get the value "query" which is entered in the search box.
            return  true;
        }
    };
    searchView.setOnQueryTextListener(queryTextListener);

    return super.onCreateOptionsMenu(menu);
}

我正在 onCreateOptionsMenu.onQueryTextSubmit 方法中获取提交文本。我现在该怎么办?

您应该使用自定义搜索视图以访问工具栏中的 EditText(app:actionViewClass 属性 在菜单布局中)。设置 TextWathcer 并在 afterTextChanged 回调内管理跨度后。

我写了一些具有基本背景 span 实现的示例。它是解决您的问题的主要概念和起点。

参见下面的示例:

menu_main.xml:

<item android:id="@+id/action_search"
    android:title="search"
    android:icon="@drawable/ic_search"
    app:showAsAction="ifRoom|collapseActionView"
    android:hint="Enter Tags"
    app:actionViewClass="your.package.CustomSearchView" />

CustomSearchView.java:

public class CustomSearchView extends SearchView {

    private AutoCompleteTextView mSearchAutoComplete;

    public CustomSearchView(Context context) {
        super(context);
        initialize();
    }

    public CustomSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public CustomSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }

    public void initialize() {
        mSearchAutoComplete = (AutoCompleteTextView) findViewById(android.support.v7.appcompat.R.id.search_src_text);

        if (mSearchAutoComplete == null) {
            Log.wtf("TEST", "Some Changes in AppCompat????");
            return;
        }
        mSearchAutoComplete.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

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

            @Override
            public void afterTextChanged(Editable s) {
                mSearchAutoComplete.removeTextChangedListener(this);
                setSpans(s, Color.RED);
                mSearchAutoComplete.addTextChangedListener(this);
            }
        });
    }

    private void setSpans(Editable s, @ColorInt int backgroundColor) {
        BackgroundColorSpan[] spans = s.getSpans(0, s.length(), BackgroundColorSpan.class);

        String[] words;
        if (s.toString().endsWith(" ")) {
            words = (s.toString() + "X").split("\s");
        } else {
            words = s.toString().split("\s");
        }
        int completedWordsCount = words.length - 1;
        if (spans.length != completedWordsCount) {
            for (BackgroundColorSpan span : spans) {
                s.removeSpan(span);
            }

            int currentIndex = 0;
            for (int i = 0; i < words.length - 1; i++) {
                s.setSpan(new BackgroundColorSpan(backgroundColor), currentIndex, currentIndex + words[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                currentIndex += words[i].length() + 1;
            }
        }
    }
}

P.S。此 link 可能对您设置可点击跨度也很有用 - Link.