簡體   English   中英

Android菜單項打開新片段

[英]android menu item open new fragment

我正在使用滑動選項卡作為導航來顯示5頁(片段),在應用程序菜單中(3點)按鈕,我想創建2頁,其中1個用於設置,另一些用於關於頁面(僅靜態文本)。 請建議我該怎么辦。

具體來說,當我從菜單項中單擊時,如何去顯示一個新的片段(不在我的標簽中)。

請幫助我,謝謝你。

到目前為止,這是我的代碼。

@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);
    }


}

}

不知道這是否是您想要的,但是我有一個片段可以打開一個新的Actvity(里面有一個片段):

 public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.action_about:
         Intent intent_to = new Intent(getActivity(),MyNewActivity.class);
            startActivity(intent_to);
            break;

    }
    return true;
}

MyNewActivity類中,您應該有一個容器並使用getFragmentManager()打開一個片段

如果要根據新片段的關閉方式進行操作,甚至可以使用startActivityForResult

希望能幫助到你

問題是WTH,您可以使用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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM