Toolbar button click event functionality

后端 未结 3 665
醉话见心
醉话见心 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 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);
        }
    

提交回复
热议问题