-1
Calendar myCalendar = Calendar.getInstance();

DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
    // TODO Auto-generated method stub
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    }

};

edittext.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        new DatePickerDialog(new_split.this, date, myCalendar
            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
            myCalendar.get(Calendar.DAY_OF_MONTH)).show();
    }
});

private void updateLabel() {

String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

edittext.setText(sdf.format(myCalendar.getTime()));
}
4

2 回答 2

7

很难确切地说出你在问什么,但如果你想隐藏CalendarViewin DatePickerDialog,以下将起作用:

if (Build.VERSION.SDK_INT >= 11) {
    datePickerDialog.getDatePicker().setCalendarViewShown(false);
}

(在 API 级别低于 11getDatePicker()且不setCalendarViewShown()可用时,这无关紧要 -CalendarView无论如何,对话框中都没有。)

于 2013-09-05T17:43:50.403 回答
0
>  got this worked by defining the datepicker dialog as a different
> class by inheriting it and by defining interface....
public class MainActivity extends Activity implements DatePickerFragment.TheListener{
edittext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
}
});






public class DatePickerFragmenta extends DialogFragment implements DatePickerDialog.OnDateSetListener {
  TheListenera listenera;

  public interface TheListenera{
      public void returnaDate(String date);


  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {

      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);
      listenera = (TheListenera) getActivity(); 

      DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
      dialog.getDatePicker().setCalendarViewShown(false);
      dialog.setTitle("Pick a date");
      return dialog; 
  }
于 2013-09-26T11:16:03.983 回答