Custom search in Android?

后端 未结 1 1271
失恋的感觉
失恋的感觉 2021-01-16 01:59

I have been working on Implementing Custom search in Android App-bar. So, I have added following code in menu_main.xml

         


        
相关标签:
1条回答
  • 2021-01-16 02:40

    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.

    See example below:

    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. This link might be also useful for you in order to set Clickable spans - Link.

    0 讨论(0)
提交回复
热议问题