我使用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级我希望通过对话框片段实现相同但没有得到任何线索如何开始,任何帮助都将受到高度赞赏
答案 0 :(得分:0)
原因是什么?
http://developer.android.com/reference/android/app/Activity.html#showDialog(int)
Android DialogFragment vs Dialog
如何解决?
使用新的DialogFragment类代替FragmentManager;这也可以通过Android兼容包在旧版平台上使用。
http://android-developers.blogspot.in/2012/05/using-dialogfragments.html