本文介绍了如何在MainActivity的ActionBar中关闭/关闭/折叠SearchView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在输入值后清除并关闭SearchView.我找到了一个解决方案,但它会关闭搜索,并且如果我尝试再次搜索,将不会执行任何操作.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_actions, menu);

    searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setOnQueryTextListener(searchListener);

    return super.onCreateOptionsMenu(menu);
}

SearchView.OnQueryTextListener searchListener = new SearchView.OnQueryTextListener(){
    @Override
    public boolean onQueryTextChange(String arg0) {
        return false;
    }
    @Override
    public boolean onQueryTextSubmit(String query) {
        new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
        try {
            searchView.onActionViewCollapsed();
        }
        catch(Exception ex){
            ex.printStackTrace();
            System.out.println(ex);
        }
        return true;
    }
};

我只能第一次搜索.每次下一次它只关闭我的输入键盘,什么也不做.如何以正确的方式执行?

编辑.建议的更改如下所示:

        try {
            searchView.setIconified(true);
        }

activity_main_actions.xml:

   <!-- Search Widget -->
   <item android:id="@+id/action_search"
       android:icon="@drawable/ic_action_search"
       android:title="@string/action_search"
       android:showAsAction="always"
       android:actionViewClass="android.widget.SearchView"/>

Edit2:

我更改了showAsAction="always|collapseActionView",但是当我单击搜索时它会关闭我的应用程序.

我还将侦听器放入OnCreateOptionsMenu中,以查看它是否发生了任何变化:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_actions, menu);
    searchMenuItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setOnQueryTextListener(new OnQueryTextListener(){

            @Override
            public boolean onQueryTextChange(String arg0) {
                return false;
            }

            @Override
            public boolean onQueryTextSubmit(String query) {
                if (searchMenuItem != null) {
                    boolean closed = searchMenuItem.collapseActionView();
                    Toast.makeText(getApplicationContext(), "closing: " + closed, Toast.LENGTH_SHORT).show();
                }
                new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
                return false;
            }
    });
    return super.onCreateOptionsMenu(menu);
}

closed布尔值是false-我不知道为什么.我阅读了文档,但它什么也没告诉我./p>

Edit3:

我在这个问题上阅读了太多的互联网,对我来说还不清楚.清除搜索小部件,隐藏键盘并执行操作.唯一的问题是搜索不会折叠,因此它涵盖了ActionBar上其余的操作按钮.

            @Override
            public boolean onQueryTextSubmit(String query) {
                searchView.setIconified(true);
                searchView.clearFocus();
                new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
                return false;
            }

在这里我读到crashActionView()无效,因为我的搜索不是视图而是窗口小部件./p>

解决方案

使用:

searchMenuItem.collapseActionView();

代替:

searchView.onActionViewCollapsed();

如果您使用的是AppCompat library,请使用:

MenuItemCompat.collapseActionView(searchMenuItem);

I'm trying to clear and close SearchView after entering a value. I found a solution but it closes the search and won't perform any action if I try to search again.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_actions, menu);

    searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setOnQueryTextListener(searchListener);

    return super.onCreateOptionsMenu(menu);
}

SearchView.OnQueryTextListener searchListener = new SearchView.OnQueryTextListener(){
    @Override
    public boolean onQueryTextChange(String arg0) {
        return false;
    }
    @Override
    public boolean onQueryTextSubmit(String query) {
        new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
        try {
            searchView.onActionViewCollapsed();
        }
        catch(Exception ex){
            ex.printStackTrace();
            System.out.println(ex);
        }
        return true;
    }
};

I can search only for the first time. Every next time it only closes my input keyboard and does nothing. How can it be performed in a right way?

Edit.Suggested change looks like this:

        try {
            searchView.setIconified(true);
        }

activity_main_actions.xml:

   <!-- Search Widget -->
   <item android:id="@+id/action_search"
       android:icon="@drawable/ic_action_search"
       android:title="@string/action_search"
       android:showAsAction="always"
       android:actionViewClass="android.widget.SearchView"/>

Edit2:

I changed showAsAction="always|collapseActionView" but it closes my app when I click the search.

I also put the listener inside OnCreateOptionsMenu to see if it changes anything:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_actions, menu);
    searchMenuItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setOnQueryTextListener(new OnQueryTextListener(){

            @Override
            public boolean onQueryTextChange(String arg0) {
                return false;
            }

            @Override
            public boolean onQueryTextSubmit(String query) {
                if (searchMenuItem != null) {
                    boolean closed = searchMenuItem.collapseActionView();
                    Toast.makeText(getApplicationContext(), "closing: " + closed, Toast.LENGTH_SHORT).show();
                }
                new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
                return false;
            }
    });
    return super.onCreateOptionsMenu(menu);
}

And the closed boolean is false - I don't know why. I read the doc but it tells me nothing.

Edit3:

I've read too much Internet on this subject and it is not clear for me yet. Search widget is cleared, keyboard is hidden and action is performed. The only issue is the search isn't collapsed so it covers the rest of action buttons on the ActionBar.

            @Override
            public boolean onQueryTextSubmit(String query) {
                searchView.setIconified(true);
                searchView.clearFocus();
                new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
                return false;
            }

Here I read that collapseActionView() will not work, because my Search is not a view but a widget.

解决方案

Use:

searchMenuItem.collapseActionView();

Instead of:

searchView.onActionViewCollapsed();

If you are using AppCompat library, then use:

MenuItemCompat.collapseActionView(searchMenuItem);

这篇关于如何在MainActivity的ActionBar中关闭/关闭/折叠SearchView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:12