2015-10-23 93 views
1

我一直致力於實施Custom searchAndroid應用程序欄。所以,我在menu_main.xml在Android中進行自定義搜索?

<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添加下面的代碼。它看起來像這樣:

enter image description here

這是完全正常的!我還沒有實現SearchView以上的後端代碼。現在,我想在上面Search bar

  • 用戶添加一些以下特點可以用標籤搜索(使用標籤搜索意味着,如果有兩個詞之間的空間,然後我就會把它們當做標籤)。這與Pintreset應用非常相似。

  • 此外我想從Search Bar得到這些標籤,並把Get請求參數。

    截圖

enter image description here

問題

  • 現在,忘了Style。我只想知道如何在我的Search Bar中實現此目標?
  • 我該如何在每個標籤上加上這個十字標記的框Android app 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方法。我現在應該怎麼做?

回答

1

您應該使用自定義搜索視圖才能訪問工具欄中的EditText(菜單佈局中的app:actionViewClass屬性)。設置TextWathcer並管理afterTextChanged回調內的跨度。

我寫了一些基本背景跨度實現的例子。這是解決您的問題的主要概念和起點。

參見下例:

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

+0

感謝您的回答。將'mSearchAutoComplete =(AutoCompleteTextView)findViewById(android.support.v7.appcompat.R.id.search_src_text);'將是'mSearchAutoComplete =(AutoCompleteTextView)findViewById(android.support.v7.appcompat.R.id.search_src_text); '? –

+0

我沒有顯示任何'AutoCompleteTextView'。用戶將添加一些帶空格的字符串。之後,我需要採取這些措辭並進行查詢 –

+0

@AmitPal是的,你不顯示,但這是它如何在Android源代碼內部實現。看看這裏 - http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/widget/SearchView.java#116。 – Oleksandr