2013-12-17 39 views
0

我想擴展SearchView類以覆蓋onKeyDown()方法。所以,我做我的片段類中,如下:如何在片段中使用擴展SearchView

public class myfragment extends ListFragment{ 
... 
class CustomSearchView extends SearchView{ 
    public CustomSearchView(Context context) { 
     super(context); 
    }  

    public CustomSearchView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event){ 
     Toast.makeText(getActivity(), "onkeydown", Toast.LENGTH_SHORT).show(); 
     return super.onKeyDown(keyCode, event); 
    } 
} 
... 
} 

現在我想利用在片段這個擴展搜索查看的。但我不知道該怎麼做。由於我使用searchItem.getActionView()創建SearchView對象,有沒有辦法將其轉換爲CustomSearchView對象,以便在我的片段中觸發被覆蓋的onKeyDown方法?

@Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    ... 

      // Retrieves the system search manager service 
      final SearchManager searchManager = 
        (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE); 

      // Retrieves the SearchView from the search menu item 
      final SearchView searchView = (SearchView) searchItem.getActionView(); 

      // Assign searchable info to SearchView 
      searchView.setSearchableInfo(
        searchManager.getSearchableInfo(getActivity().getComponentName())); 

      searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
       @Override 
       public boolean onQueryTextSubmit(String queryText) { 
        // Nothing needs to happen when the user submits the search string 
        return true; 
       } 

       @Override 
       public boolean onQueryTextChange(String newText) { 
        ... 
       } 
      }); 

這個問題突然出現,因爲在How can I get onBackPressed() while SearchView is activated?

回答

0

提到的解決方案,您可以投放到您的自定義SearchView組成:

final CustomSearchView searchView = (CustomSearchView) searchItem.getActionView(); 

在聲明SearchView您在使用聲明自己的搜索查看在meny xml文件中提供完整的類名。另外,如果您正在使用支持庫,您必須聲明http://schemas.android.com/apk/res-auto命名空間:

不支持庫:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" > 

<item android:id="@+id/action_search" 
     android:title="@string/action_search" 
     android:icon="@drawable/action_search" 
     android:showAsAction="always|collapseActionView" 
     android:actionViewClass="com.xxx.ListFragment.CustomSearchView" /> 

支持庫:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:yourapp="http://schemas.android.com/apk/res-auto" > 

<item android:id="@+id/action_search" 
     android:title="@string/action_search" 
     android:icon="@drawable/action_search" 
     yourapp:showAsAction="always|collapseActionView" 
     yourapp:actionViewClass="com.xxx.ListFragment.CustomSearchView" /> 
+0

菜單吹氣拋出一類未發現異常在類「com.example.xxx.PhoneContactListFragment.CustomSearchView」的運行時。我不知道我在這裏做錯了什麼。我使用「android:showAsAction」,而不是「yourapp:showAsAction」。與「android:actionViewClass」相同。因爲如果我使用「yourapp:..」,編輯器顯示錯誤「找不到包含'com.example.xxx'屬性'showAsAction'的資源標識符'」 – faizal

+0

您可能是對的,因爲我使用支持庫,我想你別。使用'android'命名空間。至於錯誤,你的班級是否公開。也許你可以把它放在片段類之外? – Szymon

+0

你是對的!我沒有使用支持庫,所以我使用'android'命名空間。將CustomSearchView類放在它自己的java文件中解決了錯誤。但是,當我觸摸「SearchView」或軟鍵盤上的任何字符時,「onKeyDown」並沒有被觸發。在片段類的搜索視圖中定義的「onQueryTextLListener」按預期觸發。但不明白爲什麼'onKeyDown'根本不會被調用。 – faizal

相關問題