2014-01-21 186 views
0

我試圖實現與搜索領域的操作欄,根據官方Android政黨成員(http://developer.android.com/training/search/setup.html安卓操作欄的搜索按鈕

這裏是我的文件:

清單文件:

<application 
... 
    android:theme="@style/Theme.AppCompat.Light" > 

<activity 
      android:name=".activities.MainActivity" 
      android:label="@string/title_activity_main" > 
      <meta-data 
       android:name="android.app.searchable" 
       android:resource="@xml/searchable" /> 
     </activity> 

    <activity 
     android:name=".activities.SearchActivity" 
     android:configChanges="orientation|keyboardHidden" 
     android:screenOrientation="portrait" 
     android:windowSoftInputMode="stateHidden" > 
      <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 
    </activity> 

... 

</application> 

(元數據存在)

MainActivity:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.action_bar_menu, menu); 

    //Searchable configuration 
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); //MenuItemCompat. 
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 

    return super.onCreateOptionsMenu(menu); 

} 

action_bar_menu.xml:

<item 
    android:id="@+id/search" 
    android:icon="@drawable/ic_action_search" 
    android:title="@string/menu_search" 
    yourapp:actionViewClass="android.support.v7.widget.SearchView" 
    yourapp:showAsAction="always"/> 

RES/XML/searchable.xml:

<?xml version="1.0" encoding="utf-8"?> 
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
    android:hint="my hint" 
    android:label="@string/app_name" /> 

操作欄和搜索按鈕被顯示。我可以輸入數據,但點擊搜索按鈕(虛擬鍵盤)時,沒有任何附加內容。

任何想法? 謝謝

回答

1

使用此代碼

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.activity_sky_reader_android, menu); 

    SearchView mSearchView = (SearchView) menu.findItem(R.id.menu_search); 
    setupSearchView(mSearchView); 
    return true; 

} 


private void setupSearchView(final MenuItem searchItem) { 

    searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM 
       | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); 
    mSearchView.setOnQueryTextListener(new OnQueryTextListener() { 

     @Override 
     public boolean onQueryTextSubmit(String query) { 
      // TODO Auto-generated method stub 

      return true; 
     } 

     @Override 
     public boolean onQueryTextChange(String newText) { 
      // TODO Auto-generated method stub 
      return true; 
     } 
    }); 
} 
+0

感謝您的回覆。它適用於「setOnQueryListener」,但我想使用「android.intent.action.SEARCH」。 – johann

1

上搜索的標籤應該是相同的塔爾的活動之一,嘗試在清單

<activity 
     android:name=".activities.SearchActivity" 
     android:configChanges="orientation|keyboardHidden" 
     android:screenOrientation="portrait" 
     android:label="@string/app_search" 
     android:windowSoftInputMode="stateHidden" > 
      <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 
    </activity> 

在可搜索

<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
    android:hint="my hint" 
    android:label="@string/app_search" /> 

希望它有效