1

是否可以創建DialogFragment並更改默認容器? 我試過DialogFragment.show(Transaction, ...)並在那裏設置容器,但它告訴我Fragment already added。 DialogFragment的行爲與正常的DialogFragment相似,這一點很重要。爲DialogFragment選擇容器

編輯:我覺得有一些誤解。我說它應該看起來像「一個普通的DialogFragment」。我的意思是應該看起來像一個正常的AlertDialog。

+0

添加一些更多的細節,如代碼,異常消息。並且是「默認容器」與自定義對話框視圖有關。 – Pravin 2014-11-03 10:28:43

+0

這是一個普遍的問題。我想要在其他任何地方都有鎖屏。所以我需要將我的DialogFragments放置在一個容器中,它位於lockscreencontainer的下面。 – Kuno 2014-11-03 10:47:05

+0

鎖屏是小部件,當屏幕鎖定時調用它,我們使用getWindow()。addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); – 2014-11-05 13:40:35

回答

1

我想因爲DialogFragmentFragment延伸,你可以使用它作爲一個正常Fragment在任何需要的地方。它仍應實施所有Fragment生命週期方法。但是,我不確定它是否爲特殊目的而覆蓋其中的任何一個。

0

我這裏是從DialogFragment

public static class MyDialogFragment extends DialogFragment { 
     int mNum; 

    /** 
    * Create a new instance of MyDialogFragment, providing "num" 
    * as an argument. 
    */ 
    static MyDialogFragment newInstance(int num) { 
     MyDialogFragment f = new MyDialogFragment(); 

     // Supply num input as an argument. 
     Bundle args = new Bundle(); 
     args.putInt("num", num); 
     f.setArguments(args); 

     return f; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mNum = getArguments().getInt("num"); 

     // Pick a style based on the num. 
     int style = DialogFragment.STYLE_NORMAL, theme = 0; 
     switch ((mNum-1)%6) { 
      case 1: style = DialogFragment.STYLE_NO_TITLE; break; 
      case 2: style = DialogFragment.STYLE_NO_FRAME; break; 
      case 3: style = DialogFragment.STYLE_NO_INPUT; break; 
      case 4: style = DialogFragment.STYLE_NORMAL; break; 
      case 5: style = DialogFragment.STYLE_NORMAL; break; 
      case 6: style = DialogFragment.STYLE_NO_TITLE; break; 
      case 7: style = DialogFragment.STYLE_NO_FRAME; break; 
      case 8: style = DialogFragment.STYLE_NORMAL; break; 
     } 
     switch ((mNum-1)%6) { 
      case 4: theme = android.R.style.Theme_Holo; break; 
      case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break; 
      case 6: theme = android.R.style.Theme_Holo_Light; break; 
      case 7: theme = android.R.style.Theme_Holo_Light_Panel; break; 
      case 8: theme = android.R.style.Theme_Holo_Light; break; 
     } 
     setStyle(style, theme); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // put here any view you want 
     View v = inflater.inflate(R.layout.fragment_dialog, container, false); 
     View tv = v.findViewById(R.id.text); 
     ((TextView)tv).setText("Dialog #" + mNum + ": using style " 
       + getNameForNum(mNum)); 

     // Watch for button clicks. 
     Button button = (Button)v.findViewById(R.id.show); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       // When button is clicked, call up to owning activity. 
       ((FragmentDialog)getActivity()).showDialog(); 
      } 
     }); 

     return v; 
    } 
} 



void showDialog() { 
    mStackLevel++; 

    // DialogFragment.show() will take care of adding the fragment 
    // in a transaction. We also want to remove any currently showing 
    // dialog, so make our own transaction and take care of that here. 
    FragmentTransaction ft = getFragmentManager().beginTransaction(); 
     // may you got error here 
    Fragment prev = getFragmentManager().findFragmentByTag("dialog"); 
    if (prev != null) { 
    // remove fragment from stack will fix the problem 
     ft.remove(prev); 
    } 
    ft.addToBackStack(null); 

    // Create and show the dialog. 
    DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel); 
    newFragment.show(ft, "dialog"); 
} 

,如果你想在報警按鈕使用它這裏是代碼

public static class MyAlertDialogFragment extends DialogFragment { 

    public static MyAlertDialogFragment newInstance(int title) { 
     MyAlertDialogFragment frag = new MyAlertDialogFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("title", title); 
     frag.setArguments(args); 
     return frag; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     int title = getArguments().getInt("title"); 

     return new AlertDialog.Builder(getActivity()) 
       .setIcon(R.drawable.alert_dialog_icon) 
       .setTitle(title) 
       .setPositiveButton(R.string.alert_dialog_ok, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
          ((FragmentAlertDialog)getActivity()).doPositiveClick(); 
         } 
        } 
       ) 
       .setNegativeButton(R.string.alert_dialog_cancel, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
          ((FragmentAlertDialog)getActivity()).doNegativeClick(); 
         } 
        } 
       ) 
       .create(); 
    } 
} 
+0

alertdialog按鈕怎麼樣?我是否必須模仿我的佈局中的外觀? – Kuno 2014-11-05 13:22:01

+0

不,你可以隨意使用它,閱讀文檔,你會發現關於這個的例子,我也會爲你更新答案 – 2014-11-05 13:24:51

+0

這是行不通的。如果您嵌入了DialogFragment,則不會調用onCreateDialog。所以要麼你可以選擇一個容器,但不能有一個本地的alertDialog,或者你可以有一個本地的AlertDialog,但是不能將它嵌入到你的佈局中。它是否正確? – Kuno 2014-11-05 13:51:30

1

你可以使用dialogFragment像一個普通的片段。看看樣本中的這段代碼。 在此代碼中,它創建一個dialogFragment並將其添加到framlayout,並在按下按鈕時將其顯示爲對話框。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:padding="4dip" 
    android:gravity="center_horizontal" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <TextView 
      android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:gravity="top|center_horizontal" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Demonstrates the same fragment 
      being shown as a dialog and embedded inside of an activity." /> 

    <Button android:id="@+id/show_dialog" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:text="show"> 
     <requestFocus /> 
    </Button> 

    <View android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

    <TextView 
      android:id="@+id/inline_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Fragment embedded inside 
     of the activity:" /> 

    <FrameLayout 
      android:id="@+id/embedded" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:padding="6dp" 
      android:background="#ff303030" 
      android:gravity="top|center_horizontal" /> 

</LinearLayout> 

和java代碼:

public class FragmentDialogOrActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_dialog_or_activity); 

     if (savedInstanceState == null) { 
      // First-time init; create fragment to embed in activity. 

      FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      DialogFragment newFragment = MyDialogFragment.newInstance(); 
      ft.add(R.id.embedded, newFragment); 
      ft.commit(); 

     } 

     // Watch for button clicks. 
     Button button = (Button)findViewById(R.id.show_dialog); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       showDialog(); 
      } 
     }); 
    } 


    void showDialog() { 
     // Create the fragment and show it as a dialog. 
     DialogFragment newFragment = MyDialogFragment.newInstance(); 
     newFragment.show(getFragmentManager(), "dialog"); 
    } 



    public static class MyDialogFragment extends DialogFragment { 
     static MyDialogFragment newInstance() { 
      return new MyDialogFragment(); 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View v = inflater.inflate(R.layout.hello_world, container, false); 
      View tv = v.findViewById(R.id.text); 
      ((TextView)tv).setText("This is an instance of MyDialogFragment"); 
      return v; 
     } 
    } 

} 

,併爲你的錯誤,你可以使用下面的代碼:你注意到我改變了標籤名稱,以便您可以對話框區分

public static void showMyDialogFragment(FragmentManager fm){ 

    FragmentTransaction ft = fm.beginTransaction(); 
    Fragment prev = fm.findFragmentByTag("MyDialogFragment"); 
    if (prev != null) { 
     ft.remove(prev); 
    } 
    ft.addToBackStack(null); 
    DialogFragment newFragment = MyDialogFragment.newInstance(); 
    newFragment.show(ft, "MyDialogFragment"); 
} 

片段和普通片段添加到您的佈局。

+0

看看我的op編輯。我的意思是它應該看起來像一個alertdialog。我想在你的例子中你只有一個textView。沒有標題,沒有對話框按鈕。如果你說DialogFagment.show,你不能挑選容器。所以這是行不通的。 – Kuno 2014-11-05 13:48:26

+1

當我使用MyDialogFragment擴展DialogFragment時,這意味着它可以是任何你喜歡的東西。你可以添加按鈕到片段佈局創建自定義標題和你可以想象的一切。這只是一個例子,你可以用你喜歡的任何東西來替換'R.layout.hello_world'。 – mmlooloo 2014-11-05 13:58:53

+0

所以我必須爲我的標題和按鈕收集所有需要的樣式,讓它看起來像一個AlertDialog? – Kuno 2014-11-05 14:05:40