0

これらのコード Eclipse の行で、「DialogFragment を View にキャストできません」というような 3 番目のエラーが表示されますが、回避策はありますか? または他のオプション/オプション?

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

1 に答える 1

1

これは、TimePickerDialog を表示するための 1 つの方法です。

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 に答える