在android的警报管理器中的对话片段,它不工作

时间:2016-05-20 04:54:52

标签: android

public  class MainActivity extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
    // Use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
}
public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");
}}

1 个答案:

答案 0 :(得分:3)

DialogFragment中,您必须使用onCreateView而不是setContentView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.d(TAG, "onCreateView");
    View view = inflater.inflate(R.layout.activity_main, container,
            false);

    return view;
}

TimePickerFragment使用TimePickerFragment.newInstance()致电并newInstance

需要DialogFragment构造函数
        DialogFragment dialogFrag = null;
        dialogFrag = TimePickerFragment.newInstance();
        dialogFrag.setTargetFragment(this, request_id);
        dialogFrag.show(getChildFragmentManager().beginTransaction(),
                "timepicker_dialog");