Toolbar button click event functionality

后端 未结 3 637
醉话见心
醉话见心 2021-01-29 16:35

I have created settings button in Toolbar, now I need to navigate the screen to settings screen, when ever i click the settings button.

相关标签:
3条回答
  • 2021-01-29 16:51

    Try this:

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.your_menu_xml, menu);
            return true;
        }
    
     public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()){
                case R.id.action_settings:
                    //Strar activity here
                    break;
                default:
                    return super.onOptionsItemSelected(item);
            }
            return super.onOptionsItemSelected(item);
        }
    
    0 讨论(0)
  • 2021-01-29 17:01

    You can use onOptionsItemSelected function. This reference will help you do so.

    0 讨论(0)
  • 2021-01-29 17:10

    you should override the onCreateOptionsMenu and onOptionsItemSelected methods and take that particular id from menuitem and navigate to settings screen.

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.your_menu_xml, menu);
            return true;
        }
    
    @Override
     public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()){
                case R.id.action_settings:
                    Intent i = new Intent(yourActivity.this, SettingsActivity.class)
                    startActivity(i);
                    break;
                default:
                    return super.onOptionsItemSelected(item);
            }
            return super.onOptionsItemSelected(item);
        }
    
    0 讨论(0)
提交回复
热议问题