2012-08-10 148 views
9

我這裏有代碼的選項菜單上安卓的DatePicker和DatePicker的對話框

Dialog dialog = new Dialog(ScheduleActivity.this); 
dialog.setTitle("Add Event"); 
dialog.setContentView(R.layout.add_even_on); 
Button datePicker = (Button) dialog.findViewById(R.id.datePicker); 
final DialogFragment dateFrag = new MyDatePicker(); 
    datePicker.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       dateFrag.show(getSupportFragmentManager(), "datePicker");    
      } 
     }); 
dialog.show(); 

時,說「添加事件」選項菜單上點擊時,會出現一個對話框,有一個按鈕,顯示DatePickerDialog並在它旁邊是一個TextView,它反映了DatePickerDialog中選擇的日期,這裏是我從Androids Developer獲得的關於如何使用DatePickerDialog的類。

class MyDatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener { 
int pYear; 
int pDay; 
int pMonth; 

@Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // Use the current date as the default date in the picker 
    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); 

    // Create a new instance of DatePickerDialog and return it 
    return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 

    public void onDateSet(DatePicker view, int year, int month, int day) { 
    pYear = year; 
    pDay = day; 
    pMonth = month; 
    } 
} 

所以我的問題是,我如何獲得milleseconds值當我點擊「設置」就這又自動關閉它的DatePickerDialog,並返回到我的對話,其中包含打開DatePickerDialog和按鈕一個TextView反映選擇了DatePickerDialog watever日期...我不會顯示一個我選擇了DatePickerDialog內...

這裏是我的意思的圖片,

因此,當我上挑點擊日期按鈕DatePickerDialog框出現,如下圖所示 enter image description here

enter image description here

,當我點擊設置,我想那DatePickerDialog

回答

13

我在這裏提供一些代碼希望它有助於您考慮在millseconds值從..

public class NewReminder extends Activity { 
private static final int DATE_DIALOG_ID = 1; 
private int year; 
private int month; 
private int day; 
EditText editTextDate; 
private String currentDate; 
private Context context; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.addnewreminder); 
    initialize(); 
    context = getApplicationContext(); 
    OnClickListener listenerDate = new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      final Calendar c = Calendar.getInstance(); 
      year = c.get(Calendar.YEAR); 
      month = c.get(Calendar.MONTH); 
      day = c.get(Calendar.DAY_OF_MONTH); 
      showDialog(DATE_DIALOG_ID); 
     } 
    }; 
    editTextDate.setOnClickListener(listenerDate); 

} 

private void initialize() { 
    // TODO Auto-generated method stub 

    editTextDate = (EditText) findViewById(R.id.editTextDate); 

} 


private void updateDisplay() { 
    currentDate = new StringBuilder().append(day).append(".") 
      .append(month + 1).append(".").append(year).toString(); 

    Log.i("DATE", currentDate); 
} 

OnDateSetListener myDateSetListener = new OnDateSetListener() { 

    @Override 
    public void onDateSet(DatePicker datePicker, int i, int j, int k) { 

     year = i; 
     month = j; 
     day = k; 
     updateDisplay(); 
     editTextDate.setText(currentDate); 
    } 
}; 



@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DATE_DIALOG_ID: 
     return new DatePickerDialog(this, myDateSetListener, year, month, 
       day); 
    } 
    return null; 
} 
} 
+0

這是最接近我做什麼,我艱難的做了一些修改,我並沒有完全使用所有的方法,但它足夠接近礦井.. tnx – lemoncodes 2012-08-10 06:31:15

+2

showDialog(DATE_DIALOG_ID);已棄用。使用對話框片段代替http://stackoverflow.com/questions/11220820/the-method-showdialogint-from-the-type-activity-is-deprecated-in-android http://stackoverflow.com/questions/12710092/how - 使用片段管理員對sobstitute-depri-showdialog – 2013-05-03 15:36:40

13

舊對話框已被棄用。實現片段:

遷移您的活動片段活動:

public class YourActivity extends FragmentActivity 

創建片段對話:

public class TimePickerFragment extends DialogFragment { 

    private OnDateSetListener listener; 

    public TimePickerFragment(OnDateSetListener listener) { 
     this.listener=listener; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current time as the default values for the picker 
     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); 

     // Create a new instance of TimePickerDialog and return it 
     return new DatePickerDialog(getActivity(), listener, year,month,day); 
    } 

} 

實現接口(包:進口android.app.DatePickerDialog.OnDateSetListener):

public class YourActivity extends FragmentActivity implements OnDateSetListener{ 

    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) { 

    } 

} 

添加showDialog函數:

public class YourActivity extends FragmentActivity implements OnDateSetListener{ 

    showDateDialog(){ 

     FragmentManager fm = getSupportFragmentManager(); 
     TimePickerFragment newFragment = new TimePickerFragment(this); 
     newFragment.show(fm, "date_picker"); 

    } 

    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) { 

    } 

} 
+0

工程就像一個魅力。 – 2014-09-16 20:39:19

+0

如果下次有人單擊按鈕以顯示片段,我想讓日期成爲用戶選擇的最後日期,該怎麼辦? – 2015-11-18 04:37:56

+0

您用newInstance squema發送參數或初始參數:http://developer.android.com/reference/android/app/DialogFragment.html – Hpsaturn 2015-11-19 22:16:49

1

你可以試試這個代碼..它一定會對你有所幫助..沒有疑問!

protected Dialog onCreateDialog(int id) { 

    Calendar c=Calendar.getInstance(); 
    int Sysday=c.get(Calendar.DAY_OF_MONTH); 
    int Sysmonth=c.get(Calendar.MONTH); 
    int Sysyear=c.get(Calendar.YEAR); 

    int Sysmin=c.get(Calendar.MINUTE); 
    int Syshour=c.get(Calendar.HOUR); 


    switch (id) { 
    case TIME_DIALOG: 

     return new TimePickerDialog(this, myTimePicker , Syshour, Sysmin, false); 

    case DATE_DIALOG: 
     return new DatePickerDialog(this, myDatePicker, Sysyear, Sysmonth, Sysday); 
    } 
    return null; 
} 
1

這是我的幫助功能,獲取上下文和TextView的作爲參數,並設置對文本視圖日期當用戶選擇一個日期:

 public static void showDate(final Context context, final TextView textView) { 

    if (textView != null) { 
     final 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); 
       String myFormat = "MM/dd/yy"; // In which you need put here 
       SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); 
       //UIHelper.showLongToastInCenter(context, sdf.format(myCalendar.getTime())); 
       textView.setText(sdf.format(myCalendar.getTime())); 
      } 

     }; 
     new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show(); 
    } else { 
     UIHelper.showLongToastInCenter(context, "Unable to show Date picker"); 
    } 
} 
4

在XML中添加一個TextView和一個Button

在Java文件
<TextView 
    android:id="@+id/searchText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

<Button 
    android:id="@+id/search" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Search" /> 

添加以下代碼

public class DatePickerDialogExample extends Activity { 

    TextView txtDate; 
    private int mYear, mMonth, mDay, mHour, mMinute; 

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


     txtDate = (TextView) findViewById(R.id.searchText); 
     Button search = (Button) findViewById(R.id.search); 
     search.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // Process to get Current Date 
      final Calendar c = Calendar.getInstance(); 
      mYear = c.get(Calendar.YEAR); 
      mMonth = c.get(Calendar.MONTH); 
      mDay = c.get(Calendar.DAY_OF_MONTH); 

      // Launch Date Picker Dialog 
      DatePickerDialog dpd = new DatePickerDialog(PrayTimeActivity.this, 
        new DatePickerDialog.OnDateSetListener() { 

         @Override 
         public void onDateSet(DatePicker view, int year, 
           int monthOfYear, int dayOfMonth) { 
          // Display Selected date in textbox 
          txtDate.setText(dayOfMonth + "-" 
            + (monthOfYear + 1) + "-" + year); 

         } 
        }, mYear, mMonth, mDay); 
       dpd.show(); 
      } 
     }); 


    } 

} 
0
Calendar c1 = Calendar.getInstance();  
int year = c1.get(Calendar.YEAR); 
int month = c1.get(Calendar.MONTH); 
int day = c1.get(Calendar.DAY_OF_MONTH); 
DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() { 

     public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) { 
        if(arg0.isShown()){ 

            //do further code here 

           } 
          } 

          }; 
     DatePickerDialog dp = new DatePickerDialog(YourActivity.this, myDateListener, year, month, day); 
     dp.show();  
1

試試這個代碼是肯定的工作:

  final Calendar c = Calendar.getInstance(); 
      mYear = c.get(Calendar.YEAR); 
      mMonth = c.get(Calendar.MONTH); 
      mDay = c.get(Calendar.DAY_OF_MONTH); 

      DatePickerDialog dpd = new DatePickerDialog(getActivity(), 
        new DatePickerDialog.OnDateSetListener() { 

         @Override 
         public void onDateSet(DatePicker view, int year, 
           int monthOfYear, int dayOfMonth) { 

          birth_Date.setText(dayOfMonth + "-" 
            + (monthOfYear + 1) + "-" + year); 
         } 
        }, mYear, mMonth, mDay); 
      dpd.show();