如何在点击时编辑动态日期选择器中隐藏日历视图

时间:2013-09-05 17:28:05

标签: android

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()));
}

2 个答案:

答案 0 :(得分:7)

很难确切地说出您的要求,但如果您要隐藏CalendarView中的DatePickerDialog,以下内容将有效:

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

(在11级以下getDatePicker()setCalendarViewShown()不可用的API级别无关紧要 - 无论如何,对话框中都没有CalendarView。)

答案 1 :(得分: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; 
  }