3

我正在嘗試創建一個類似於DatePickerDialog的簡單對話框。我正在創建的Dialog應該爲用戶提供一組可供用戶選擇的圖像。定製對話框片段

我相信我已經設法創建了陣列並向它添加了正確的圖像,我現在遇到的問題是如何讓Dialog顯示出來?我應該返回什麼?

我已經調查過AlertDialogs等,但我不知道該怎麼做才能實現它們。

更新:固定的問題,CODE下面顯示的是運作

PICTURE PICKER

public class PicturePickerFragment extends DialogFragment { 

ArrayList<Integer> imageList = new ArrayList<Integer>(); 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // fill an array with selected images 

    String title = "Picture"; 

    imageList.add(R.drawable.barbershop); 
    imageList.add(R.drawable.wedding); 
    imageList.add(R.drawable.meeting); 
    imageList.add(R.drawable.barbershop); 

    // return alertdialog 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    builder.setView(inflater.inflate(R.layout.activity_create, null)) 
      .setTitle(R.string.event_type) 
      .setPositiveButton(R.string.select_picture, 
        new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, 
           int which) { 
          // call the method on the parent activity when 
          // user click the positive button 
         } 
        }); 
    return builder.create(); 
} 

} 

FOR REFERENCE

public class DatePickerFragment extends DialogFragment implements 
    DatePickerDialog.OnDateSetListener { 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // Use the current date as the default date in the picker 
    final Calendar c = Calendar.getInstance(); 
    int year = c.get(Calendar.YEAR); 
    int month = c.get(Calendar.MONTH); 
    int day = c.get(Calendar.DAY_OF_MONTH); 

    // Create a new instance of DatePickerDialog and return it 
    return new DatePickerDialog(getActivity(), this, year, month, day); 
} 

public void onDateSet(DatePicker view, int year, int month, int day) { 
    // Do something with the date chosen by the user 
    month++; 
    String dateString = month + "/" + day + "/" + year; 
    Button b = (Button) getActivity().findViewById(R.id.btn_date); 
    b.setText(dateString); 
} 
} 

片段將被用來LIKE本

<Button 
    android:id="@+id/btn_picture" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="15dp" 
    android:layout_marginTop="15dp" 
    android:layout_weight="1" 
    android:background="@null" 
    android:drawablePadding="15dp" 
    android:onClick="showPicturePickerDialog" 
    android:drawableTop="@drawable/ico_picture" 
    android:text="Picture" 
    android:textColor="@color/darkbrown" 
    android:textSize="20sp" /> 
+0

請許多例子指http://developer.android.com/reference/android/app/DialogFragment.html這個鏈接,並張貼問題 –

回答

5

如果要在其中打開DialogFragment延伸FragmentActivity的活動,你應該執行:

PicturePickerFragment dialog = new PicturePickerFragment(); 
dialog.show(getSupportFragmentManager(), "YourDialog"); 

還需要誇大你的對話框佈局文件在對話框片段類的方法onCreateDialog

使用AlertDialog.Builder您可以輕鬆實現這一切,例如,

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

LayoutInflater inflater = getActivity().getLayoutInflater(); 

// Inflate and set the layout for the dialog 
// Pass null as the parent view because its going in the dialog layout 
builder.setView(inflater.inflate(R.layout.dialoglayout, null)) 
.setTitle(R.string.dialog_title) 
.setPositiveButton(R.string.pos_button, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     // call the method on the parent activity when user click the positive button 
    } 
}) 
.setNegativeButton(R.string.neg_button, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     // call the method on the parent activity when user click the negative button 
    } 
}); 
return builder.create(); 

上有http://developer.android.com/reference/android/app/DialogFragment.html

+0

完善之前就谷歌。我期待現在添加一個用戶可以從中選擇的圖片數組。當我使用.setView()時,我應該使用一個具有ListView的佈局來存放圖像數組或者什麼? –

+0

是的你的對話框佈局應該包含一個ListView。您可以使用自定義適配器來填充列表,該適配器擴展了包含圖像數組的ArrayAdapter(http://developer.android.com/reference/android/widget/ArrayAdapter.html)。如果您需要幫助,請提出另一個問題,這是一個完全不同的主題:) –

1

有不同的方式,你可以創建一個使用OnCreateView方法或使用OnCreateDialog方法,如果您正在擴展DialogFragment類等的實施中去兩個不同的方式你的對話中使用。

您應該從OnCreateDialog方法中返回Dialog實例。

如何顯示在Activity

FragmentTransaction ft = getFragmentManager().beginTransaction(); 
Fragment prev = getFragmentManager().findFragmentByTag("YourFragmentID"); 

if (prev != null) { 
     ft.remove(prev); 
    } 
ft.addToBackStack(null); 

PicturePickerFragment pf = new PicturePickerFragment(); 
pf.show(); 

對話框可能是你可以參考這個link這是來自官方AndroidDeveloper的網站一個很好的例子。