
我有一个简单的DatePickerDialog,打开EditText时打开.选择日期并按OK后,它应显示在同一EditText中.如果我只使用默认对话框,它只创建一个按钮就可以正常工作 – 好的.我添加了一个取消按钮,问题是它只获取当前日期.
这是我的代码:
private voID showDatePicker(String birthdayStr) { // Todo auto-generated method stub final Calendar c = Calendar.getInstance(); if (birthdayStr.equals("")) { yearStr = c.get(Calendar.YEAR); monthStr = c.get(Calendar.MONTH); dayStr = c.get(Calendar.DAY_OF_MONTH); } DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog Box is closed, below method will be called. public voID onDateSet(DatePicker vIEw, int selectedYear, int selectedMonth, int selectedDay) { if (isOkayClicked) { birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay); yearStr = selectedYear; monthStr = selectedMonth; dayStr = selectedDay; } isOkayClicked = false; } }; DatePickerDialog datePickerDialog = new DatePickerDialog( RegistrationTwoActivity.this, datePickerListener, yearStr, monthStr, dayStr); datePickerDialog.setbutton(DialogInterface.button_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int which) { if (which == DialogInterface.button_NEGATIVE) { dialog.cancel(); isOkayClicked = false; } } }); datePickerDialog.setbutton(DialogInterface.button_POSITIVE, "OK", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int which) { if (which == DialogInterface.button_POSITIVE) { isOkayClicked = true; birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay); } } }); datePickerDialog.setCancelable(false); datePickerDialog.show(); }@H_419_9@如果我删除了birthday.setText行(selectedYear(selectedMonth 1)selectedDay);在OK或button_POSITIVE下,它工作正常.但是在某些设备上,它没有将所选日期设置为EditText,因为它只在datePickerListener中调用.所以我决定添加行birthday.setText(selectedYear(selectedMonth 1)selectedDay);在OK或button_POSITIVE下但现在的问题是它只获取当前日期.
我有点困惑.如果有人可以帮助我.
解决方法:
在代码中进行以下更改
final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog Box is closed, below method will be called. public voID onDateSet(DatePicker vIEw, int selectedYear, int selectedMonth, int selectedDay) { if (isOkayClicked) { birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay); yearStr = selectedYear; monthStr = selectedMonth; dayStr = selectedDay; } isOkayClicked = false; } }; final DatePickerDialog datePickerDialog = new DatePickerDialog( RegistrationTwoActivity.this, datePickerListener, yearStr, monthStr, dayStr); datePickerDialog.setbutton(DialogInterface.button_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int which) { if (which == DialogInterface.button_NEGATIVE) { dialog.cancel(); isOkayClicked = false; } } }); datePickerDialog.setbutton(DialogInterface.button_POSITIVE, "OK", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int which) { if (which == DialogInterface.button_POSITIVE) { isOkayClicked = true; DatePicker datePicker = datePickerDialog .getDatePicker(); datePickerListener.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth()); } } }); datePickerDialog.setCancelable(false); datePickerDialog.show();@H_419_9@要使用此代码,您应该在Manifest中将minSdkVersion更改为至少11.希望对你有帮助.. :)
总结以上是内存溢出为你收集整理的android – DatePickerDialog确定按钮未获取所选日期全部内容,希望文章能够帮你解决android – DatePickerDialog确定按钮未获取所选日期所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)