我在ActionBar中实现了SearchView,但问题是,无论何时输入内容并按提交,SearchView都不会折叠回搜索图标,它会保持展开状态,当我按下后退按钮时,SearchView不会不要倒退了。
我的实现是这样的:
menu_search.xml
<item
android:id="@+id/app_bar_search"
android:icon="@drawable/ic_search_white_24dp"
android:title="Search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
item = menu.findItem(R.id.app_bar_search);
searchView = (SearchView) item.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null);
searchBar = searchView.findViewById(searchBarId);
searchBar.setLayoutTransition(new LayoutTransition());
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
item.collapseActionView();
searchView.setQuery("", false);
searchView.clearFocus();
searchBar.clearFocus();
.
.
.
.
.
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
我尝试过的事情:
@Override
public void onBackPressed() {
super.onBackPressed();
if (item.isActionViewExpanded()) {
item.collapseActionView();
} else {
super.onBackPressed();
}
}
和
@Override
public void onBackPressed() {
super.onBackPressed();
if (searchView != null) {
searchView.onActionViewCollapsed();
searchView.setQuery("", false);
searchView.clearFocus();
searchBar.clearFocus();
hideSoftKeyboard(MainActivity.this);
} else {
super.onBackPressed();
}
}
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
}
SearchView从左向右扩展以进行输入,但不会从右向左折叠。
请建议。
答案 0 :(得分:0)
尝试下面的代码,让我知道它是否有效。
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, ResultActivity.class)));
searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
@Override
public boolean onSuggestionClick(int position) {
searchView.clearFocus();
item.collapseActionView();
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
item.collapseActionView();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
答案 1 :(得分:0)
我正在浏览Google搜索的第二页并找到了这个。 它对我有用。
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
searchView.setIconified(true);
}
}
});