标签:
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 action bar if it is present. //方法一、静态添加 xml方式添加菜单 // getMenuInflater().inflate(R.menu.menu_main, menu); //方法二、动态添加 代码添加菜单项 MenuItem item = menu.add(1, 100, 1, "赵1"); //参数(groupID , ItemID , order ,title) item.setTitle("aaa"); menu.add(1,101,1,"赵2"); menu.add(1,102,1,"赵3"); menu.add(1,5,1,"赵4"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a 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); //跳转到其他activity textView.setText("点击了" + id); break; } case 101:{ textView.setText("点击了" + id); break; } case 102:{ textView.setText("点击了" + id); break; } case 5:{ textView.setText("点击了" + id); break; } } return super.onOptionsItemSelected(item); } }
标签:
原文地址:http://www.cnblogs.com/zmaibbs7/p/4888015.html