2013-09-23 54 views
0

我使用顯示對話框在Android中創建日期選取器,我成功地使用它。我想日期選擇器出現在buttonClick而是通過做的ShowDialog這是過時了,我用..Android備用壓縮顯示Dilaog

Main Activity 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    final Calendar c = Calendar.getInstance(); 
    pYear= c.get(Calendar.YEAR); 
    pMonth = c.get(Calendar.MONTH); 
    pDay = c.get(Calendar.DAY_OF_MONTH); 


    setContentView(R.layout.main); 

    addListenerOnButton(); 

} 



public void addListenerOnButton() { 



    btnChangeDate = (Button) findViewById(R.id.btnChangeDate); 
    btnChangeDate.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      showDialog(DATE_DIALOG_ID); 
      /*MainActivity.dialogMaps.put(++MainActivity.dialogIdCounter, 
        new DatePickerDialog((Activity) MainActivity.this, 
          mDateSetListener, mYear, mMonth, mDay));*/ 

      /*((Activity) MainActivity.this) 
        .showDialog(MainActivity.dialogIdCounter);*/ 

     } 
    }); 

} 



@SuppressLint("NewApi") 
@Override 
protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DATE_DIALOG_ID: 
      DatePickerDialog dpDialog = new DatePickerDialog(this, mDateSetListener, pYear, pMonth, pDay); 
      DatePicker datePicker = dpDialog.getDatePicker(); 

      Calendar calendar = Calendar.getInstance();//get the current day 
      datePicker.setMaxDate(calendar.getTimeInMillis());//set the current day as the max date 
      return dpDialog; 
     } 

     return null; 
} 



protected void onPrepareDialog(int id, Dialog dialog) { 
     switch (id) { 
     case DATE_DIALOG_ID: 
       ((DatePickerDialog) dialog).updateDate(pYear, pMonth-1, pDay); 
       break; 
     } 
} 



private void updateDisplay() { 
    btnChangeDate = (Button) findViewById(R.id.btnChangeDate); 
    btnChangeDate.setText(new StringBuilder().append(pDay).append("/") 
      .append(pMonth + 1).append("/").append(pYear)); 
} 


private DatePickerDialog.OnDateSetListener mDateSetListener = 
     new DatePickerDialog.OnDateSetListener() { 
    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear, 
      int dayOfMonth) { 
     // TODO Auto-generated method stub 
     pYear = year; 
     pMonth = monthOfYear; 
     pDay = dayOfMonth; 
     updateDisplay(); 
    } 
}; 

我才知道的ShowDialog已棄用的API級別13我想實現通過對話片段相同但沒有得到任何線索如何開始,任何幫助將不勝感激

回答