简体   繁体   English

如何在Android中的Sherlock ActionBar中获取选项菜单按钮的OnClick事件?

[英]how to get the OnClick event of Option menu button in Sherlock ActionBar in Android?

I am working on Sherlock ActionBar and using an Option button at right corner, near search icon. 我正在使用Sherlock ActionBar并使用右上角的选项按钮,靠近搜索图标。 I am successful to generate the onClick event for SEARCH button but I am getting problems in onClick event of the option Button. 我成功为SEARCH按钮生成onClick事件,但我在选项Button的onClick事件中遇到问题。 Using the below code to add the buttons : 使用以下代码添加按钮:

public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        menu.add(0, 1, 1, "Search").setIcon(R.drawable.search_icon).setActionView(R.layout.action_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        menu.add(0, 2, 2, "Button").setIcon(R.drawable.menu_overflow_icon).setActionView(R.layout.action_button).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
    }

And for click events : 对于点击事件:

@Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
        switch (item.getItemId()) {
        case 1:
            search = (EditText) item.getActionView();
            search.addTextChangedListener(filterTextWatcher);
            search.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    break;
        case 2:
            Toast.makeText(ActivityScreen.this, "hit it", Toast.LENGTH_SHORT).show();


        }
        return super.onOptionsItemSelected(item);   
    }       

I have shown my requirement in the image attached below. 我在下面的图片中显示了我的要求。 在此输入图像描述

Replace your break; 替换你的休息; statements in the Switch of onOptionsItemSelected to return true; switch of onOptionsItemSelected中的语句返回true;

Addition to the answer 除了答案

final static int BUTTON_ID = 2;

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuItem item = menu.add(Menu.NONE, BUTTON_ID, Menu.NONE, R.string.action_option_name);
    item.setIcon(R.drawable.action_option);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId())
    {
    case BUTTON_ID:
        Toast.makeText(this, "clicked on 2", Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Note: I haven't added MenuItem.setActionView(R.layout.action_search). 注意:我没有添加MenuItem.setActionView(R.layout.action_search)。 I tried to use the solution at https://stackoverflow.com/a/11292843/1567588 . 我尝试使用https://stackoverflow.com/a/11292843/1567588上的解决方案。 But its not working. 但它不起作用。 And i have set GroupId and OrderId to Menu.None 我已将GroupId和OrderId设置为Menu.None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM