Android:在一个对话框中使用一个datepicker和timepicker

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

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

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

  • showDialog未定义为View.OnClickListener()
  • onCreateDialog方法:types为EventCreateDialog的onCreateDialog(int)方法必须覆盖或实现一个超types方法
  • DatePickerDialog是未定义的(因为这不是一个acitvity)

所有这些东西在一个Activity内工作,但是我不能从一个Dialog中工作。

谢谢,Luc

 包com.android.myapp;
import...
公共类TestDialog扩展Dialog实现android.view.View.OnClickListener {
私人TextView mDateDisplay;
私人buttonmPickDate;
私人buttonmPickTime;
 private int mYear;
 private int mMonth;
 private int mDay;
私人int mHour;
私人int mMinute;
 static final int DATE_DIALOG_ID = 0;
 static final int TIME_DIALOG_ID = 1;

私人buttonmButton_ok;

私人buttonmButton_ko;

私人ReadyListener readyListener;

私人上下文环境;    

    公共TestDialog(上下文上下文,ReadyListener readyListener){
        超级(上下文);
         this.context = context;
         this.readyListener = readyListener;
     }

     @覆盖
    保护无效的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(本);
         mButton_ko.setOnClickListener(本);

             mDateDisplay =(TextView)findViewById(R.id.dateDisplay);
             mPickDate =(Button)findViewById(R.id.pickDate);
             mPickTime =(Button)findViewById(R.id.pickTime);
             //将点击侦听器添加到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);  }
             });
             //获取当前date
            最终日历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();
     }   

         @覆盖
        保护对话框onCreateDialog(int id){
            开关(id){
            案例DATE_DIALOG_ID:
                返回新的DatePickerDialog(this,mDateSetListener,mYear,mMonth,mDay);
             case TIME_DIALOG_ID:
                返回新的TimePickerDialog(this,mTimeSetListener,mHour,mMinute,false);
             }
            返回null;
         }
         private void updateDisplay(){
             mDateDisplay.setText(
                新的StringBuilder()
                     //月份是0,所以加1
                     .append(mMonth + 1).append(“ - ”)
                     .append(MDAY).append( “ - ”)
                     .append(mYear).append(“”)
                     .append(垫(mHour))附加。( “:”)
                    .append(垫(mMinute)));
         }

         //当用户在对话框中设置date时收到的callback
        私人DatePickerDialog.OnDateSetListener mDateSetListener =
            新的DatePickerDialog.OnDateSetListener(){
                 public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth){
                     mYear =年份;
                     mMonth = monthOfYear;
                     mDay = dayOfMonth;
                     updateDisplay();
                 }
         };

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

         private static String pad(int c){
            如果(c> = 10)
                返回String.valueOf(c);
            其他
                返回“0”+ String.valueOf(c);
         }

    公共接口ReadyListener { 
         public void ready(MyObj myObj); 
     } 

      @覆盖
         public void onClick(View v){
        如果(v == mButton_ok){
             // 做东西....

         }


        如果(v == mButton_ko){
            解雇();
         }
      }
 }

Solutions Collecting From Web of "Android:在一个对话框中使用一个datepicker和timepicker"

以下是我的工作原理:

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 ------------------------------------------------------ } 

PickerViewSample.java

 package com.sai.samples.views; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import android.widget.TimePicker; public class PickerViewSample extends Activity { static final int DATE_DIALOG_ID = 1; static final int TIME_DIALOG_ID = 2; private TextView dateDisplay; private Button pickDate; private int year, month, day; private TextView timeDisplay; private Button pickTime; private int hours, min; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dateDisplay = (TextView)findViewById(R.id.TextView01); pickDate = (Button)findViewById(R.id.Button01); pickDate.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { showDialog(DATE_DIALOG_ID); } }); final Calendar cal = Calendar.getInstance(); year = cal.get(Calendar.YEAR); month = cal.get(Calendar.MONTH); day = cal.get(Calendar.DAY_OF_MONTH); updateDate(); timeDisplay = (TextView)findViewById(R.id.TextView02); pickTime = (Button)findViewById(R.id.Button02); pickTime.setOnClickListener( new OnClickListener () { @Override public void onClick(View v) { showDialog(TIME_DIALOG_ID); } }); hours = cal.get(Calendar.HOUR); min = cal.get(Calendar.MINUTE); updateTime(); } private void updateTime() { timeDisplay.setText(new StringBuilder().append(hours).append(':') .append(min)); } private void updateDate() { dateDisplay.setText(new StringBuilder().append(day).append('-') .append(month + 1).append('-').append(year)); } private DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int yr, int monthOfYear, int dayOfMonth) { year = yr; month = monthOfYear; day = dayOfMonth; updateDate(); } }; private TimePickerDialog.OnTimeSetListener timeListener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { hours = hourOfDay; min = minute; updateTime(); } }; protected Dialog onCreateDialog(int id){ switch(id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, dateListener, year, month, day); case TIME_DIALOG_ID: return new TimePickerDialog(this, timeListener, hours, min, false); } return null; } } 

main.xml中

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:text="@string/date_text" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="26px" android:typeface="sans"></TextView> <Button android:text="@string/date_button" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <TextView android:text="@string/time_text" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="26px" android:typeface="sans"></TextView> <Button android:text="@string/time_button" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> 

datepickerlayout.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <DatePicker android:id="@+id/DatePicker01" android:layout_width="wrap_content" android:layout_height="wrap_content"></DatePicker> </LinearLayout> 

在全局中声明variables为:

 private int year, month, day, Hour, Minute; public static final int DATE_PICKER_ID = 1111; 

在onCreate()

 // Get current date by calender final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); 

从button点击调用datepicker

 showDialog(Constant.DATE_PICKER_ID); 

方法:

  @Override protected Dialog onCreateDialog(int id) { switch (id) { case Constant.DATE_PICKER_ID: // open datepicker dialog. // set date picker for current date // add pickerListener listner to date picker return new DatePickerDialog(this, pickerListener, year, month, day); case Constant.TIME_PICKER_ID: return new TimePickerDialog(this, timeSetListener, Hour, Minute, false); } return null; } private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog box is closed, below method will be called. @Override public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { year = selectedYear; month = selectedMonth; day = selectedDay; text_date.setText(new StringBuilder().append(month + 1) .append("/").append(day).append("/").append(year) .append(" ")); } }; 

这应该工作:

 mPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { context.showDialog(DATE_DIALOG_ID); } }); 

onCreateDialog只能从一个活动中调用