Android: How to add listener to hardware menu button?

前端 未结 4 1374
时光说笑
时光说笑 2021-01-01 12:09

I\'m currently trying to add a click listener to the menu hardware button. Currently I\'m just putting my onclick logic into the onCreatePanelMenu-method and return false. B

4条回答
  •  自闭症患者
    2021-01-01 12:39

    If you need some code samples you can try this:

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.preferences:
                showPreferencesActivity();
                return true;
            case R.id.logOff:
                logOff();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    

    The above should be pretty self explanotory - it sets a menu with the options to show preferences or log off.

    /Nicklas

提交回复
热议问题