一旦搜索完成,我会尝试隐藏软键盘,但我找不到它失败的原因.
我创建了如下搜索菜单:
@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()
很好崩溃,没有问题,但我无法让软键盘关闭.
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);