2015-12-18 64 views
1

我有這個代碼工作正常,但窗口不是模態,所以我不能得到它被更改後的日期。請提前給我提供一些建議或方法。在Android中創建模式日期選擇器對話框

下面是代碼:

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

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

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

       TextView txtDate = null; 
       txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); 

      } 

      public void onDateSet1(DatePicker view, int year, 
            int monthOfYear, int dayOfMonth) { // TODO 
       // Auto-generated 
       // method stub 

      } 
     }, mYear, mMonth, mDay); 

dpd.show(); 

message("message", "Mensaje", mYear + "/" + mMonth + "/" + mDay, this, context); 
+0

你是什麼意思「改變後,我不能獲得日期」? – AdamMc331

+0

當顯示日期的窗口打開時,程序繼續運行,它不會停止,直到我獲得新的日期。該對話框不是模態的。 – user2286970

回答

1
private int storeYear = 0; 
private int storeMonth = 0; 
private int storeDay = 0; 

private void function openPicker(){ 
    final TextView txtDate = findViewById(R.id.yourid); 
    final Calendar c = Calendar.getInstance(); 
    int mYear = c.get(Calendar.YEAR); 
    int mMonth = c.get(Calendar.MONTH); 
    int mDay = c.get(Calendar.DAY_OF_MONTH); 

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

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

        //TextView txtDate = null; 
        txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) 
          + "-" + year); 
        storeYear = year;storeMonth = monthOfYear;storeDay = dayOfMonth; 

    message("message", "Mensaje", mYear + "/" + mMonth + "/" 
      + mDay, this, context); 
       } 

       public void onDateSet1(DatePicker view, int year, 
         int monthOfYear, int dayOfMonth) { // TODO 
                  // Auto-generated 
                  // method stub 

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

} 
+0

同樣的東西,'message()'這一行代表函數被調用之後的日期,日期選擇器窗口在後面,消息在前面。我希望用戶選擇日期,然後...消息。 – user2286970

+0

呵呵。這行是在對話框顯示之後,不是數據被改變。 –

+0

先選擇一個日期,然後顯示消息。就像一個模態對話框。 – user2286970

相關問題