如何onBackPressed()
在搜索模式下处理?我已经在 中实现了搜索ActionBar
,并且我想处理onBackPressed()
.
编辑:
在MainActivity
我添加了这个,但只有在搜索关闭时才会收到通知
@Override
public void onBackPressed() {
mMenu.findItem(R.id.menu_eye).setVisible(true);
mMenu.findItem(R.id.menu_layers).setVisible(true);
super.onBackPressed();
};
我的搜索操作监听器如下所示:
import com.cyrilmottier.polaris.PolarisMapView;
import android.app.SearchManager.OnCancelListener;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SearchView;
public class SearchListener implements SearchView.OnQueryTextListener, SearchView.OnCloseListener, SearchView.OnSuggestionListener, OnClickListener, OnCancelListener{
private Context mContext;
private PolarisMapView mMapView;
private Menu mMenu;
private SearchView mSearchView;
public SearchListener(Context c, PolarisMapView mMapView, Menu mMenu, SearchView mSearchView){
this.setmContext(c);
this.setmMapView(mMapView);
this.setmMenu(mMenu);
this.mSearchView = mSearchView;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
if (Constants.searchPlaceNavigate(query, mContext, mMapView))
return this.onClose();
return false;
}
public Context getmContext() {
return mContext;
}
public void setmContext(Context mContext) {
this.mContext = mContext;
}
public PolarisMapView getmMapView() {
return mMapView;
}
public void setmMapView(PolarisMapView mMapView) {
this.mMapView = mMapView;
}
@Override
public boolean onSuggestionClick(int position) {
String p = mSearchView.getSuggestionsAdapter().getCursor().getString(position);
if(position== 0)
p = mSearchView.getSuggestionsAdapter().getCursor().getString(position*4+1);
if(p != null)
if(position== 0)
Constants.searchPlaceNavigate(mSearchView.getSuggestionsAdapter().getCursor().getString(position*4+1), mContext, mMapView);
else
Constants.searchPlaceNavigate(mSearchView.getSuggestionsAdapter().getCursor().getString(position), mContext, mMapView);
return this.onClose();
}
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
@Override
public boolean onClose() {
mMenu.findItem(R.id.menu_eye).setVisible(true);
mMenu.findItem(R.id.menu_layers).setVisible(true);
mMenu.findItem(R.id.menu_search).collapseActionView();
return true;
}
public Menu getmMenu() {
return mMenu;
}
public void setmMenu(Menu mMenu) {
this.mMenu = mMenu;
}
public SearchView getmSearchView() {
return mSearchView;
}
public void setmSearchView(SearchView mSearchView) {
this.mSearchView = mSearchView;
}
@Override
public void onClick(View v) {
this.onClose();
}
@Override
public void onCancel() {
this.onClose();
}
}