问题
I have to press the backbutton twice, to close the SearchView
. Why? On the first press, the SearchView
only looses focus...
Setting setOnKeyListener
on SearchView
does not work either...
Btw, I'm using the ABS implementation...
My code is simple and looks like the following:
mMenuItemSearch = menu.findItem(R.id.search);
mSearchView = new SearchView(getSupportActionBar().getThemedContext());
mMenuItemSearch.setActionView(mSearchView);
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
public boolean onQueryTextChange(String newText)
{
mPagerManager.getFragment(mSelectedPos).adapter.getFilter().filter(newText);
return true;
}
public boolean onQueryTextSubmit(String query)
{
return true;
}
});
回答1:
After trying a lot, I found the solution.
In onQueryTextSubmit(String query)
write searchView.clearFocus();
Then in onBackPressed()
use below code:
if (!searchView.isIconified())
{
searchView.setIconified(true);
searchView.onActionViewCollapsed();
}
else
{
super.onBackPressed();
}
回答2:
onBackPressed should only clear the focus from search view (SearchAutoComplete) EditText, it is normal behavior.
But if you want to change this behavior , do it (:
final SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
final MenuItem searchViewItem = menu.findItem(R.id.searchView);
searchAutoComplete.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!searchAutoComplete.isFocused()){
searchViewItem.collapseActionView();
searchView.onActionViewCollapsed();
viewManager.dismissSearchDropDownPopupWindow();
}
}
});
回答3:
try this. It worked for me after trying lots of things.
mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
MenuItemCompat.collapseActionView(searchMenu);
}
}
});
回答4:
The search view closing on back is the expected behavior and what we have accustomed to. You could override that if you would like by extending upon your own SearchView and/or listening for a onBackPressed() to call finish on your activity that the SearchView lives within.
回答5:
I solved it this way:
...
public class MyFragment extends Fragment implemented SearchView.OnFocusChangeListener <----
...
@Override
public void onFocusChange(View v, boolean hasFocus) {
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView sv = (SearchView)MenuItemCompat.getActionView(menuItem);
if(!hasFocus) {
sv.onActionViewCollapsed(); <----
interactionListener.showDrawerToggle(true); <----
}
}
...
public void configureSearchView(Menu menu) {
this.menu = menu;
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView sv = (SearchView)MenuItemCompat.getActionView(menuItem);
sv.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
interactionListener.showDrawerToggle(false);
}
});
sv.setOnQueryTextListener(this);
sv.setOnCloseListener(this);
sv.setSubmitButtonEnabled(false);
sv.setIconifiedByDefault(true);
sv.setOnQueryTextFocusChangeListener(this); <----
if(initialQuery != null) {
sv.setIconified(false);
menuItem.expandActionView();
sv.setQuery(initialQuery, true);
}
}
...
回答6:
try this in your configureSearchView() method, it works for me.
mSearchView.post(new Runnable() {
@Override
public void run() {
mSearchView.setIconified(false);
}
});
回答7:
Only way to fix this is using android.support.v7.widget.SearchView
instead of android.widget.SearchView
I got it fixed by doing this.
回答8:
You'd need to extend SearchView
and override dispatchKeyEventPreIme
public class SearchView extends android.support.v7.widget.SearchView {
public SearchView(Context context) {
super(context);
}
public SearchView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
onActionViewCollapsed();
}
}
return super.dispatchKeyEventPreIme(event);
}
}
回答9:
Need call clearFocus()
method for SearchView
after search button clicked.
For example:
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
mSearchView.clearFocus();
//...
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
回答10:
If your SearchView
is on a fragment like mine, use following snippet in Kotlin
searchView.setOnQueryTextFocusChangeListener { _, hasFocus ->
if(!hasFocus){
// iconify the SearchView when the focus is lost
searchView.isIconified = true
}
}
Thanks to @vikoo's answer.
回答11:
You can call view.requestFocus()
to give focus to another view. This way the search view will close with one back press.
来源:https://stackoverflow.com/questions/18924445/searchview-not-closing-correctly-on-first-back-press-its-only-loosing-focus