2014-02-10 71 views
0

我想添加一個搜索小部件到我的操作欄。我已經閱讀谷歌開發人員的教程,但他們的代碼不工作..請幫助我。我的代碼如下:如何在android中的操作欄中添加搜索窗口小部件?

public boolean onCreateOptionsMenu(android.view.Menu menu) { 
    // TODO Auto-generated method stub 
    super.onCreateOptionsMenu(menu); 

    /* For The Action Bar Menu. Edit Menu For Adding icons */ 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.action_menu, menu); 
    MenuItem searchItem = menu.findItem(R.id.action_search); 
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); 
return super.onCreateOptionsMenu(menu); 
} 

首先我如下使用此代碼,但這也不起作用:

// Associate searchable configuration with the SearchView 
    /*SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     SearchView searchView = (SearchView) menu.findItem(R.id.action_search) 
       .getActionView(); 
     searchView.setSearchableInfo(searchManager 
       .getSearchableInfo(getComponentName()));*/ 

    return super.onCreateOptionsMenu(menu); 
} 

我的清單代碼:

<activity 
     android:uiOptions="splitActionBarWhenNarrow" 
     android:name="com.usable.mycodes.MainActivity" 
     android:label="@string/app_name" > 
     <meta-data 
      android:name="android.app.default_searchable" 
      android:value=".SearchResultsActivity" /> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

搜索活動結果用於處理搜索。

問題是點擊搜索按鈕後,沒有編輯的文本出現..

+0

哪裏是你的XML佈局.. @ user3291559 – PSK

+0

檢查此鏈接:http://novoda.com/blog/styling-actionbar-searchview – Lokesh

+0

不要忘記接受的答案,如果它真的幫助你出 –

回答

1

使用這樣的產品......

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu); 

    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.searchview_, menu); 
    MenuItem searchItem = menu.findItem(R.id.action_search); 
    mSearchView = (SearchView) searchItem.getActionView(); 
    SearchViewShow(searchItem); 

    return true; 
} 



private void SearchViewShow(MenuItem searchItem) { 

    if (isAlwaysExpanded()) { 
     mSearchView.setIconifiedByDefault(false); 
    } else { 
     searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM 
       | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); 
    } 

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    if (searchManager != null) { 
     List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch(); 

     SearchableInfo info = searchManager.getSearchableInfo(getComponentName()); 
     for (SearchableInfo inf : searchables) { 
      if (inf.getSuggestAuthority() != null 
        && inf.getSuggestAuthority().startsWith("applications")) { 
       info = inf; 
      } 
     } 
     mSearchView.setSearchableInfo(info); 
    } 

    mSearchView.setOnQueryTextListener(this); 
} 

public boolean onQueryTextChange(String newText) { 

    return false; 
} 

public boolean onQueryTextSubmit(String query) { 

    return false; 
} 

public boolean onClose() { 

    return false; 
} 

protected boolean isAlwaysExpanded() { 
    return false; 
} 

實現你的類SearchView.OnQueryTextListener

+0

夥計烏爾代碼也是正確的..感謝您的幫助.. –

相關問題