Android 开发:传递“菜单”课堂到其他活动
我有一个 menu.java,在这个文件中,我有几行代码允许用户按下菜单按钮,“MENU1”和“MENU2”将出现在他们的 Android 手机上。但是,我有多个java类,每个java类都是它自己的活动。我需要做什么才能在每个活动中拥有这个菜单类函数,而不将 menu.java 中的每一行代码相互放入 .java
这是我的 Menu.java
public class Menu extends Activity {
public boolean onCreateOptionsMenu(android.view.Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater a = getMenuInflater();
a.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.MENU1:
startActivity(new Intent("com.helloworld.test.MENU1"));
return true;
case R.id.MENU2:
startActivity(new Intent("com.helloworld.test.MENU2"));
return true;
}
return false;
}
}
I have a menu.java and in this file I have lines of codes to allow the user to press the menu button and "MENU1" and "MENU2" will appear on their android phone. However, I have multiple java classes, and each java classes is its own activity. What would I need to do to have this menu class function in each activity without putting every line of code in the menu.java into each other .java
This is my Menu.java
public class Menu extends Activity {
public boolean onCreateOptionsMenu(android.view.Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater a = getMenuInflater();
a.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.MENU1:
startActivity(new Intent("com.helloworld.test.MENU1"));
return true;
case R.id.MENU2:
startActivity(new Intent("com.helloworld.test.MENU2"));
return true;
}
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个基础活动来扩展所有其他活动:
You could create a base activity that all your other activities extend: