0

我在这些代码行中,eclipse 在第三行给了我一个错误,像这样说:“无法将 DialogFragment 转换为 View”,有什么解决方法吗?或其他选项/选项?

DialogFragment dateClock = new DatePickerFrag();
View homeSection = findViewById(R.layout.introducere_date);
((LinearLayout) homeSection).addView(((View) dateClock));
4

1 回答 1

1

这将是显示 TimePickerDialog 的一种方法 -

public static class TimePickerFragment extends DialogFragment
                        implements TimePickerDialog.OnTimeSetListener {

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    // 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() {
  DialogFragment newFragment = new TimePickerFragment();
  newFragment.show(getSupportFragmentManager(), "timePicker");
}
于 2012-08-12T18:37:35.490 回答