在Android的自定义搜索?自定义、Android

2023-09-12 05:05:01 作者:如花似玉

我一直在努力实施自定义搜索安卓应用吧。所以,我加入了以下code。在 menu_main.xml

I have been working on Implementing Custom search in Android App-bar. So, I have added following code in 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 。它是这样的:

Now when I am looking at HomeActivity. It's look like this:

这是完全正常的!我还没有实现后台code以上搜索查看。现在,我想添加一些以下功能上面搜索栏

It's perfectly fine!. I haven't implemented the backend code for above SearchView. Now, I want to add some following features in above Search bar

用户可以通过标签(含标签搜索意味着,如果有两个单词之间有一个空格,然后我就会把它们当做标签)。这非常类似于 Pintreset 应用程序。

另外我想这些标签由搜索栏投入获取请求参数。

Further I want to get these tags from Search Bar and put into Get request parameter.

截图

现在,忘了风格。我只是想知道我怎么能在我的搜索栏实现这一目标? 如何在增加框与此交叉的迹象每个标记 Android的应用程序栏? For now, forgot about the Style. I just want to know How can I achieve this in my Search Bar? How can I add box on every tag with this cross sign in Android app bar?

任何帮助将是AP preciable。

Any help would be appreciable.

我加入 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 方法提交文本。我现在应该怎么办?

I am getting the submitting text in onCreateOptionsMenu.onQueryTextSubmit method. What should I do now?

推荐答案

您应该使用自定义搜索视图,以获得EditText上的工具栏(应用:actionViewClass 属性菜单布局)。设置完毕后 TextWathcer 和管理内部跨度 afterTextChanged 回调。

You should use custom search view in order to access to EditText in toolbar (app:actionViewClass property in menu layout). After set TextWathcer and manage spans inside afterTextChanged callback.

我写了一些例如基本的背景跨越的实现。这是主要的概念,并在解决您的问题起点。

I wrote some example with basic background spans implementation. It is main concept and start point in solving your issue.

请参阅下面的例子:

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。此链接可能是也对您有用,以便设置可点击跨度 - 的链接。

 
精彩推荐
图片推荐