Mainactivity.class
public class Mainactivity extends Appcompatactivity {private TextView TextView; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView = (TextView) Findviewbyid (r.id.txt); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio n Bar if it is present. Add a menu//Getmenuinflater () by adding the XML method statically. Inflate (R.menu.menu_main, menu); Method Two, adding the code dynamically add the menu item MenuItem item = menu.add (1, 100, 1, "Zhao 1"); Parameters (GroupID, ItemID, order, title) Item.settitle ("AAA"); Menu.add (1,101,1, "Zhao 2"); Menu.add (1,102,1, "Zhao 3"); Menu.add (1,5,1, "Zhao 4"); return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, so Long/As you specify a the parent activity in Androidmanifest.xml. int id = item.getitemid (); Noinspection simplifiableifstatement switch (ID) {case 100:{Intent Intent = new Intent (Mainactivity.this,second_demo.class); Item.setintent (Intent); Jump to other activity Textview.settext ("clicked" + ID); Break } Case 101:{Textview.settext ("clicked" + ID); Break } Case 102:{Textview.settext ("clicked" + ID); Break } Case 5:{Textview.settext ("clicked" + ID); Break }} return super.onoptionsitemselected (item); }}
Preliminary understanding of menu items