如何将搜索添加到操作栏中

cavaler12345

任何人都知道我怎么可以添加搜索查看操作栏上的Android?我已经尝试了很多示例,并且在Google上搜索了很多时间,但是我没有找到解决问题的方法...我已经找到了如何将搜索放入操作栏中的方法,但是之后我可以不用搜索...我可以打开它。

这是活动:

public class SearchActivity extends Activity implements SearchView.OnQueryTextListener {


    private SearchView mSearchView;
    private ListView mListView;
    private final String[] mStrings = Cheeses.sCheeseStrings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.search_activity);

        mSearchView = (SearchView) findViewById(R.id.search_view);
        mListView = (ListView) findViewById(R.id.list_view);
        mListView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mStrings));
        mListView.setTextFilterEnabled(true);
        ActionBar actionBar = getActionBar();

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("");
        actionBar.setCustomView(mSearchView);
        mSearchView.setQuery("", true);
        mSearchView.setFocusable(true);
        mSearchView.setIconified(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
        setupSearchView();
    }

    private void setupSearchView() {
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setSubmitButtonEnabled(true);
        mSearchView.setQueryHint("Search Here");
    }

    public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText)) {
            mListView.clearTextFilter();
        } else {
            mListView.setFilterText(newText.toString());
        }
        return true;
    }

    public boolean onQueryTextSubmit(String query) {
        return false;
    }
}

在这里,这是我的结果搜索栏操作栏的个人。因此,如果有人知道我该如何解决该问题,请告诉我,我将非常感激能为我提供帮助。

阿洛克

覆盖onCreateOptionsMenu方法以通过搜索设置操作栏。此代码段应为您提供帮助。

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_list, menu);
        final MenuItem searchItem = menu.findItem(R.id.action_search);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                //Fetch the data remotely
                FetchData(query);

                //Reset the SearchView
                searchView.clearFocus();
                searchView.setQuery("", false);
                searchView.setIconified(true);
                searchItem.collapseActionView();

                //Set Activity title to search query
                MainActivity.this.setTitle(query);

                return true;
            }

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

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将搜索添加到操作栏?

如何将搜索栏添加到JSQMessagesViewController

Smartface将搜索栏添加到操作栏

如何将计算添加到gnome搜索栏?

如何将分隔线添加到拆分操作栏

如何将操作栏从支持库添加到PreferenceActivity?

如何将NetBeans(9)预定义的Maven操作添加到NetBeans工具栏?

如何将标签栏添加到其他页面,而不是在标签栏菜单中

如何将基于情节提要的页眉和CustomTableCell添加到“搜索栏和搜索显示控制器”

如何将图像添加到工具栏

如何将宏添加到进度栏

如何将工具栏添加到PreferenceActivity

如何将命令栏添加到MasterDetailsView?

将标签添加到操作栏

如何将范围搜索条件添加到API?

如何将图像和边框添加到 Android 中的侧边导航栏

如何将类添加到 Wordpress 导航栏中的最后一个 <li>?

如何将数据操作添加到$ resource对象?

如何将JavaScript变量添加到表单操作

如何将输入中的输入值添加到表单操作中?

如何将 pg_search 添加到 Solidus 前端的搜索表单中?

如何将 OR AND 添加到 Google Web Script 的搜索结果中

如何将TLD添加到Safari中的搜索例外

如何将默认@timestamp添加到我的弹性搜索数据中

如何将例外列表添加到字符串搜索中?

如果使用fontawesome图标集,如何将操作按钮添加到免费的jqgrid工具栏

如何将 antd 设计中的 popconfirm 添加到文件删除操作

激活订阅后如何将操作项添加到订阅中?

如何将PNG添加到android中的浮动操作按钮