以编程方式关闭searchview

时间:2014-10-26 14:42:25

标签: android

我有一个searchview.Here是代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    searchItem = menu.findItem(R.id.action_search);
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); 
    searchView = (SearchView)searchItem.getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    searchView.setQueryHint(getResources().getString(R.string.hint)); //if you want a custom hint
    searchView.setOnQueryTextListener(this);
    EditText et = (EditText)searchView.findViewById(searchView.getContext().getResources()
            .getIdentifier("android:id/search_src_text", null, null));
    et.setHintTextColor(getResources().getColor(R.color.white));

    if (hideSearchButton == false) {
        menu.findItem(R.id.action_search).setVisible(true);
        menu.findItem(R.id.action_shuffle).setVisible(true);
    }else{
        menu.findItem(R.id.action_shuffle).setVisible(false);
        menu.findItem(R.id.action_search).setVisible(false);
    }
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean queryTextFocused) {
            if(!queryTextFocused) {
            }
        }
    });
    return true;
}

我必须从另一种方法关闭这个搜索。我怎么能这样做?我研究了另一个问题,但没有奏效。

1 个答案:

答案 0 :(得分:4)

在你的案例中使用MenuItem.collapseActionView() searchItem.collapseActionView()