2012-05-14 44 views
22

Tôi đang viết lại đơn đăng ký của mình với hỗ trợ Fragments API. Trong ứng dụng ban đầu tôi có một AlertDialog mà được tạo ra như thế này:DialogFragment: Sử dụng AlertDialog với bố cục tùy chỉnh

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.button_dialog, null); 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setView(view); 
    ListView mListView = (ListView) view.findViewById(R.id.mblist); 
    builder.setTitle(getString(R.string.sysinfo)).setNeutralButton(
      getString(R.string.okay), null); 
    Cursor dcursor = propManager.propCursor(); 
    if (dcursor.moveToFirst()) { 
     startManagingCursor(dcursor); 
     String[] from = { Constants.NAME_DET, 
       Constants.VALUE_DET }; 
     int[] to = { R.id.mbname, R.id.mbvalue }; 
     SimpleCursorAdapter dadapter = new SimpleCursorAdapter(this, 
       R.layout.info_list_row, dcursor, from, to); 
     mListView.setAdapter(dadapter); 
    } 
    final Dialog dialog = builder.create(); 
    dialog.show(); 

Làm thế nào tôi có thể hiển thị hộp thoại tương tự thông qua một DialogFragment? Tôi đã đọc tài liệu nhưng gặp khó khăn trong việc tìm hiểu cách thực hiện điều này.

Trả lời

43

Tôi ngạc nhiên vì không ai trả lời. Đây là giải pháp:

public class PropDialogFragment extends DialogFragment { 

    private PropDialogFragment() { /*empty*/ } 

    /** creates a new instance of PropDialogFragment */ 
    public static PropDialogFragment newInstance() { 
     return new PropDialogFragment(); 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     //getting proper access to LayoutInflater is the trick. getLayoutInflater is a     //Function 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 

     View view = inflater.inflate(R.layout.my_dialog, null); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setView(view); 
     builder.setTitle(getActivity().getString(R.string.sysinfo)).setNeutralButton(
       getActivity().getString(R.string.okay), null); 
     return builder.create(); 
    } 
} 

Bạn có thể hiển thị hộp thoại sử dụng:

private void showDialog() { 
    // 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 = getSupportFragmentManager().beginTransaction(); 
    Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog"); 
    if (prev != null) { 
     ft.remove(prev); 
    } 
    ft.addToBackStack(null); 

    // Create and show the dialog. 
    DialogFragment newFragment = PropDialogFragment.newInstance(); 
    newFragment.show(ft, "dialog"); 
} 
+0

Điều này đã giúp ích rất nhiều. Cảm ơn. – BruceHill

+1

Bạn có muốn 'getActivity(). GetLayoutInflater()' thay vì gọi dịch vụ hệ thống và truyền không? –

+0

Câu trả lời thú vị. Chỉ trong trường hợp ai đó tò mò, [các tài liệu bao gồm điều này] (https://developer.android.com/reference/android/app/DialogFragment.html). – Project

1

Về cơ bản bạn chỉ có thể hiển thị Fragment của bạn như thế này:

SherlockDialogFragment newFragment = new TimePickerFragment(); 
newFragment.show(getActivity().getSupportFragmentManager(), "timePicker"); 

Và đây sẽ là một TimePickerFragment đơn giản :

public class TimePickerFragment extends SherlockDialogFragment implements TimePickerDialog.OnTimeSetListener{ 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     final Calendar c = Calendar.getInstance(); 
     int hour = c.get(Calendar.HOUR_OF_DAY); 
     int minute = c.get(Calendar.MINUTE); 


     return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); 
} 
} 
Các vấn đề liên quan