如何使用片段的扩展搜索查看 [英] how to use an extend SearchView in fragment
问题描述
我要扩展SearchView类覆盖的onkeydown()方法。所以,我做我的片段类中,如下:
I want to extend the SearchView class to override the onKeyDown() method. So i do it within my fragment class as below :
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()的搜索查看对象,是有一些办法可以将它转换为一个CustomSearchView对象,以便重写的onkeydown的方法获取我的片段触发?
Now i want to make use of this extended SearchView in the fragment. But i am not sure how to do that. Since i create the SearchView object using searchItem.getActionView(), is there some way i can cast it to a CustomSearchView object so that the overriden onKeyDown method gets triggered in my fragment?
@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) {
...
}
});
此问题杀出因为<一提到解的href=\"http://stackoverflow.com/questions/13626756/how-can-i-get-onback$p$pssed-action-while-searchview-is-activated\">How我可以得到onBack pressed行动,而搜索查看被激活?
推荐答案
您可以转换您的自定义搜索查看
:
You can cast to your custom SearchView
:
final CustomSearchView searchView = (CustomSearchView) searchItem.getActionView();
在声明搜索查看
你在MENY XML文件中使用完整的类名来声明自己的搜索查看。另外,如果您正在使用支持库,您必须声明 http://schemas.android.com/apk/res-auto
命名空间:
When you declare the SearchView
you have to declare your own SearchView using full class name in the meny xml file. Also, if you're using support library, you have to declare http://schemas.android.com/apk/res-auto
namespace:
没有支持库:
<?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" />
这篇关于如何使用片段的扩展搜索查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!