我正在使用 Show Dialog 在 Android 中创建日期选择器,我成功地使用了它。我希望日期选择器出现在 buttonClick 上,但通过已弃用的 showDialog 完成,我正在使用..
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 弃用 我想通过对话框片段实现相同但不知道如何开始,任何帮助将不胜感激