android菜单项打开新片段
我使用滑动标签作为导航来显示5页(片段),在应用程序菜单(3点)按钮中,我想创建2页,1页用于设置,另一页用于关于页面(仅静态文本)。请告诉我如何才能做到这一点 具体来说,当从菜单项单击时,如何转到要显示的新片段(不在我的选项卡中) 请帮助我,提前谢谢你 这是到目前为止我的代码android菜单项打开新片段,android,menu,android-fragments,Android,Menu,Android Fragments,我使用滑动标签作为导航来显示5页(片段),在应用程序菜单(3点)按钮中,我想创建2页,1页用于设置,另一页用于关于页面(仅静态文本)。请告诉我如何才能做到这一点 具体来说,当从菜单项单击时,如何转到要显示的新片段(不在我的选项卡中) 请帮助我,提前谢谢你 这是到目前为止我的代码 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); i
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.action_reset:
Unsafe.uhs1a.setSelection(0);
Unsafe.uhs1b.setSelection(0);
Unsafe.uhs1c.setSelection(0);
Precondition.phs1a.setSelection(0);
Precondition.phs1b.setSelection(0);
Precondition.phs1c.setSelection(0);
case R.id.action_about:
// need to open a static page ( fragment) here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
不确定这是否是您想要的,但我有一个片段可以打开一个新的活动(其中包含一个片段): 在
MyNewActivity
类中,您应该有一个容器,并使用getFragmentManager()打开一个片段
如果您想根据新片段的关闭方式执行某些操作,甚至可以使用startActivityForResult
希望这有助于解决老问题。您可以使用FragmentManager
和她的朋友FragmentTransaction
:
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.action_reset:
Unsafe.uhs1a.setSelection(0);
Unsafe.uhs1b.setSelection(0);
Unsafe.uhs1c.setSelection(0);
Precondition.phs1a.setSelection(0);
Precondition.phs1b.setSelection(0);
Precondition.phs1c.setSelection(0);
case R.id.action_about:
Fragment newFragment = new TheFragmentYouWantToOpen();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
//frame_container is the id of the container for the fragment
return true;
default:
return super.onOptionsItemSelected(item);
}
您需要使用FragmentManager设置片段事务。这方面有很多教程。
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.action_reset:
Unsafe.uhs1a.setSelection(0);
Unsafe.uhs1b.setSelection(0);
Unsafe.uhs1c.setSelection(0);
Precondition.phs1a.setSelection(0);
Precondition.phs1b.setSelection(0);
Precondition.phs1c.setSelection(0);
case R.id.action_about:
Fragment newFragment = new TheFragmentYouWantToOpen();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
//frame_container is the id of the container for the fragment
return true;
default:
return super.onOptionsItemSelected(item);
}