搜索到搜索视图后,我无法隐藏我的键盘

时间:2017-03-08 07:05:21

标签: android

    final android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    searchView.setFocusable(false);

    searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(searchView.getWindowToken(),
                    InputMethodManager.RESULT_UNCHANGED_SHOWN);


            if (query.length() >= 3) {

                String urlfilter = "http://frenzinsoftwares.in/alert/apis/search.php" +
                        "?key=" + query.toString();

                if (isNetworkAvailable()) {
                    new GetMyAppliancesfilter().execute(urlfilter);
                } else {
                    Toast.makeText(ListActivity.this, "No Network Available",
                            Toast.LENGTH_LONG).show();
                }
            }

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {


            if (isSearch) {
                if (isNetworkAvailable()) {

                    if (newText.length() == 0) {

                        String url2 = "http://frenzinsoftwares.in/alert/apis/filter_by_category.php" +
                                "?category=" + selcategory.toString() +
                                "&city=" + 1;

                        new Filtercategory().execute(url2);
                    }
                } else {
                    Toast.makeText(ListActivity.this, "No Network Available",
                            Toast.LENGTH_LONG).show();
                }

            } else {

                isSearch = true;

            }

            if (newText.length() >= 3) {

                String urlfilter = "http://frenzinsoftwares.in/alert/apis/search.php" +
                        "?key=" + newText.toString();

                if (isNetworkAvailable()) {
                    new GetMyAppliancesfilter().execute(urlfilter);
                } else {
                    Toast.makeText(ListActivity.this, "No Network Available",
                            Toast.LENGTH_LONG).show();
                }
            }
            return false;


        }
    });

3 个答案:

答案 0 :(得分:1)

在onQueryTextSubmit中尝试这个:

searchView.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        InputMethodManager imm = (InputMethodManager) searchView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                        imm.hideSoftInputFromWindow(searchView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                                    }
                                }, 50);  

尝试以下:

searchView.clearFocus();
                if (getCurrentFocus()!=null){
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                }

答案 1 :(得分:0)

尝试使用通用方式隐藏键盘:

View activeView = activityContext.getCurrentFocus();
if (activeView != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(activeView.getWindowToken(), 0);
}

答案 2 :(得分:0)

创建隐藏keyboard的方法并将其调用:

private void hideKeyboard() {
    View view = this.getActivity().getCurrentFocus();
    if (view != null) {
        view.clearFocus();
        InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
相关问题