一旦搜索完成,我会尝试隐藏软键盘,但我不知道它失败的原因。
我创建了如下搜索菜单:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
String[] mSubActionsTitles = getResources().getStringArray(R.array.sub_actions_array);
MenuItem item = menu.add(0, R.id.search, 0, mSubActionsTitles[0]);
searchMenuItem = item;
item.setIcon(android.R.drawable.ic_menu_search);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
mSearchView = new SearchView(getActivity());
mSearchView.setOnQueryTextListener(this);
mSearchView.setOnCloseListener(this);
mSearchView.setIconifiedByDefault(true);
mSearchView.setQueryHint(getString(R.string.label_tvGivePosition));
item.setActionView(mSearchView);
}
输入文本后,我启动AsyncTask()
执行HTTP查询。
然后,我们收到响应,我调用代表我试图隐藏键盘:
public synchronized void sitesLoadingFinish(Integer result) {
searchMenuItem.collapseActionView();
View view = activity.getCurrentFocus();
view.clearFocus();
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
所以searchMenuItem.collapseActionView()
很好地崩溃了,没问题,但我无法关闭软键盘。
答案 0 :(得分:6)
View view = activity.getCurrentFocus();
view .postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(view, 0);
}
},50);
答案 1 :(得分:1)
尝试使用相同的SearchView对象隐藏键盘
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
答案 2 :(得分:-1)
//试试这个
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}