在对话框中使用datepicker和timepicker

在对话框中使用datepicker和timepicker

本文介绍了Android:在对话框中使用datepicker和timepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一个问题,我不知道如何解决...
我有一个活动,当我点击链接到这个活动的菜单的一个特定的项目,一个对话框显示并用于添加一个物品。这个项目有一个日期和时间,但是我在这个对话框中没有设置一个DatePicker和TimePicker。我也尝试将活动传递给对话框,并使用它来显示datepicker,但这不起作用。
在此之前,我在另一个Activity中处理了这样的项目的创建。在这种情况下,它工作正常。但是我发现对话性别... :-)
你有什么想法吗?
希望我不太困惑.....
感谢很多,
Luc



我编辑这篇文章来分享代码我有困难。



我有一个基本的Dialog类试图使用DatePicker和TimePicker。基本上,Eclipse抱怨说:




  • showDialog未定义为View.OnClickListener()

  • onCreateDialog方法:类型为EventCreateDialog的方法onCreateDialog(int)必须重写或实现超类型方法

  • DatePickerDialog未定义(因为这不是acitvity)



    • 所有这些东西都是从一个活动中起作用的,但我不能使用对话框。



      非常感谢,
      Luc

       
      package com.android.myapp;
      import ...
      public class TestDialog extends Dialog实现android.view.View.OnClickListener {
      private TextView mDateDisplay;
      私人按钮mPickDate;
      私人按钮mPickTime;
      private int mYear;
      private int mMonth;
      private int mDay;
      private int mHour;
      private int mMinute;
      static final int DATE_DIALOG_ID = 0;
      static final int TIME_DIALOG_ID = 1;

      私人按钮mButton_ok;

      私人按钮mButton_ko;

      private ReadyListener readyListener;

      私有上下文上下文;

      public TestDialog(Context context,ReadyListener readyListener){
      super(context);
      this.context = context;
      this.readyListener = readyListener;
      }

      @Override
      protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.event_create);

      mButton_ok =(Button)findViewById(R.id.button_ok);
      mButton_ko =(Button)findViewById(R.id.button_ko);

      //添加监听器
      mButton_ok.setOnClickListener(this);
      mButton_ko.setOnClickListener(this);

      mDateDisplay =(TextView)findViewById(R.id.dateDisplay);
      mPickDate =(Button)findViewById(R.id.pickDate);
      mPickTime =(Button)findViewById(R.id.pickTime);
      //添加点击监听器到按钮
      mPickDate.setOnClickListener(new View.OnClickListener(){
      public void onClick(View v){showDialog(DATE_DIALOG_ID);}
      });
      mPickTime.setOnClickListener(new View.OnClickListener(){
      public void onClick(View v){showDialog(TIME_DIALOG_ID);}
      });
      //获取当前日期
      final Calendar c = Calendar.getInstance();
      mYear = c.get(Calendar.YEAR);
      mMonth = c.get(Calendar.MONTH);
      mDay = c.get(Calendar.DAY_OF_MONTH);
      mHour = c.get(Calendar.HOUR_OF_DAY);
      mMinute = c.get(Calendar.MINUTE);
      updateDisplay();
      }

      @Override
      protected Dialog onCreateDialog(int id){
      switch(id){
      case DATE_DIALOG_ID:
      return new DatePickerDialog (这个,mDateSetListener,mYear,mMonth,mDay);
      case TIME_DIALOG_ID:
      return new TimePickerDialog(this,mTimeSetListener,mHour,mMinute,false);
      }
      返回null;
      }
      private void updateDisplay(){
      mDateDisplay.setText(
      new StringBuilder()
      //月份为0,因此添加1
      .append ($ 1).append( - )
      .append(mDay).append( - )
      .append(mYear).append()
      .append pad(mHour))。append(:)
      .append(pad(mMinute)));
      }

      //当用户设置对话框中的日期时收到的回调
      private DatePickerDialog.OnDateSetListener mDateSetListener =
      new DatePickerDialog.OnDateSetListener(){
      public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth){
      mYear = year;
      mMonth = monthOfYear;
      mDay = dayOfMonth;
      updateDisplay();
      }
      };

      private TimePickerDialog.OnTimeSetListener mTimeSetListener =
      new TimePickerDialog.OnTimeSetListener(){
      public void onTimeSet(TimePicker view,int hourOfDay,int minute){
      mHour = hourOfDay ;
      mMinute =分钟;
      updateDisplay();
      }
      };

      private static String pad(int c){
      if(c> = 10)
      return String.valueOf(c);
      else
      返回0+ String.valueOf(c);
      }

      public interface ReadyListener {
      public void ready(MyObj myObj);
      }

      @Override
      public void onClick(View v){
      if(v == mButton_ok){
      // Do stuff ...

      }


      if(v == mButton_ko){
      dismiss();
      }
      }
      }


      解决方案

      这是我的工作原理:

        public class EditRecordDialog extends Dialog {
      protected Context _context;
      私人记录_record;

      public EditRecordDialog(Context context,
      Record record){
      super(context);
      _context = context;
      _record = record
      按钮buttonDate;
      buttonDate.setText(_record.getRecordDate()); //返回'mm-dd-yy'

      } // EditRecordDialog


      // showDatePickerDialog ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ view){
      String dateString;
      int年,月,日;

      dateString = buttonDate.getText()。toString();
      month = Integer.valueOf(dateString.substring(0,2)) - 1; //月是零基础
      day = Integer.valueOf(dateString.substring(3,5));
      year = Integer.valueOf(20+ dateString.substring(6,8));

      DatePickerDialog dpd = new DatePickerDialog(_context,dateSetListener,年,月,日);
      dpd.show();

      } // showDatePickerDialog -------------------------------------- --------------


      // OnDateSetListener +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++
      DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener( ){
      @Override public void onDateSet(DatePicker view,int year,int month,int day){
      buttonDate.setText(ConfigObjectDAO.formatDate((month + 1)+ - + day + - +年));
      } // onDateSet
      }; // OnDateSetListener ----------------------------------------------- -------
      }


      I am facing a problem I do not know how to solve...I have an activity, when I click on a particuliar item of the menu linked to this activity, a dialog is displayed and used to add an item. This item has a date and a time, but I do not manage to have a DatePicker and TimePicker within this dialog. I also try passing the activity to the dialog and use this one to display the datepicker., but that does not work.Before this, I handled the creation of such items within another Activity. In this case, it works fine. But I found the Dialog sexier... :-)Would you have any ideas ?Hope I am not too confused.....thanks a lot,Luc

      I edit this post to share the code I have difficulties with.

      I have a basic Dialog class that tried to use DatePicker and TimePicker. Basically, Eclipse complains that:

      • the showDialog is undefined for View.OnClickListener()
      • onCreateDialog method: The method onCreateDialog(int) of type EventCreateDialog must override or implement a supertype method
      • DatePickerDialog is undefined (as this is not an acitvity)

      All this stuff works from within an Activity but I cannot have it working from a Dialog.

      Thanks a lot,Luc

       package com.android.myapp;
      import ...
      public class TestDialog extends Dialog implements android.view.View.OnClickListener{
      private TextView mDateDisplay;
      private Button mPickDate;
      private Button mPickTime;
      private int mYear;
      private int mMonth;
      private int mDay;
      private int mHour;
      private int mMinute;
      static final int DATE_DIALOG_ID = 0;
      static final int TIME_DIALOG_ID = 1;
      
      private Button mButton_ok;
      
      private Button mButton_ko;
      
      private ReadyListener readyListener;
      
      private Context context;
      
          public TestDialog(Context context, ReadyListener readyListener) {
              super(context);
              this.context = context;
              this.readyListener = readyListener;
          }
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.event_create);
      
              mButton_ok = (Button)findViewById(R.id.button_ok);
              mButton_ko = (Button)findViewById(R.id.button_ko);
      
              // Add listeners
              mButton_ok.setOnClickListener(this);
              mButton_ko.setOnClickListener(this);
      
                  mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
                  mPickDate = (Button) findViewById(R.id.pickDate);
                  mPickTime = (Button) findViewById(R.id.pickTime);
                  // add a click listener to the button
                  mPickDate.setOnClickListener(new View.OnClickListener() {
                      public void onClick(View v) { showDialog(DATE_DIALOG_ID); }
                  });
                  mPickTime.setOnClickListener(new View.OnClickListener() {
                      public void onClick(View v) { showDialog(TIME_DIALOG_ID); }
                  });
                  // get the current date
                  final Calendar c = Calendar.getInstance();
                  mYear = c.get(Calendar.YEAR);
                  mMonth = c.get(Calendar.MONTH);
                  mDay = c.get(Calendar.DAY_OF_MONTH);
                  mHour = c.get(Calendar.HOUR_OF_DAY);
                  mMinute = c.get(Calendar.MINUTE);
                  updateDisplay();
          }
      
              @Override
              protected Dialog onCreateDialog(int id) {
                  switch (id) {
                  case DATE_DIALOG_ID:
                      return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
                  case TIME_DIALOG_ID:
                      return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);
                  }
                  return null;
              }
              private void updateDisplay() {
                  mDateDisplay.setText(
                      new StringBuilder()
                          // Month is 0 based so add 1
                          .append(mMonth + 1).append("-")
                          .append(mDay).append("-")
                          .append(mYear).append(" ")
                          .append(pad(mHour)).append(":")
                         .append(pad(mMinute)));
              }
      
              // the callback received when the user "sets" the date in the dialog
              private DatePickerDialog.OnDateSetListener mDateSetListener =
                  new DatePickerDialog.OnDateSetListener() {
                      public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                          mYear = year;
                          mMonth = monthOfYear;
                          mDay = dayOfMonth;
                          updateDisplay();
                      }
              };
      
              private TimePickerDialog.OnTimeSetListener mTimeSetListener =
                  new TimePickerDialog.OnTimeSetListener() {
                      public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                          mHour = hourOfDay;
                          mMinute = minute;
                          updateDisplay();
                      }
              };
      
              private static String pad(int c) {
                  if (c >= 10)
                      return String.valueOf(c);
                  else
                      return "0" + String.valueOf(c);
              }
      
          public interface ReadyListener {
              public void ready(MyObj myObj);
          }
      
           @Override
              public void onClick(View v) {
              if (v == mButton_ok) {
                  // Do stuff....
      
              }
      
      
              if(v == mButton_ko){
                  dismiss();
              }
           }
      }
      
      
      解决方案

      Here's what I have that works:

      public class EditRecordDialog extends Dialog {
        protected Context _context;
        private   Record  _record;
      
        public EditRecordDialog( Context context,
                                 Record  record ) {
          super( context );
          _context = context;
          _record  = record
          Button buttonDate;
          buttonDate.setText( _record.getRecordDate() ); // returns 'mm-dd-yy'
      
        } // EditRecordDialog
      
      
      // showDatePickerDialog ++++++++++++++++++++++++++++++++++++++++++++++++++++++
      private void showDatePickerDialog( View view ) {
        String dateString;
        int    year, month, day;
      
        dateString = buttonDate.getText().toString();
        month = Integer.valueOf( dateString.substring( 0, 2 ) ) - 1; // month is zero based
        day   = Integer.valueOf( dateString.substring( 3, 5 ) );
        year  = Integer.valueOf( "20" + dateString.substring( 6, 8 ) );
      
        DatePickerDialog dpd = new DatePickerDialog( _context, dateSetListener, year, month, day );
        dpd.show();
      
      } // showDatePickerDialog ----------------------------------------------------
      
      
      // OnDateSetListener +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override public void onDateSet( DatePicker view, int year, int month, int day ) {
          buttonDate.setText( ConfigObjectDAO.formatDate( (month+1) + "-" + day + "-" + year ) );
        } // onDateSet
      }; // OnDateSetListener ------------------------------------------------------
      }
      

      这篇关于Android:在对话框中使用datepicker和timepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:57