我已经创建了一个自定义日期和时间选择器,并且我在我的应用中的四个位置使用相同的选择器。但使用不同的按钮名称。代码非常大,所以我想创建一个单独的类,并将该类中的选择器放在一个函数中。但是在这样做之后我无法返回当前日期,只要我从另一个类调用该函数,它就会在选择日期和时间之前立即返回null。 这是自定义DateTime Picker Class:
public class DateTimePicker {
String str,samay;
public String returnDate(final Context ctx)
{
AlertDialog.Builder builder1 = new AlertDialog.Builder(ctx);
final DatePicker picker1 = new DatePicker(ctx);
try {
Field f[] = picker1.getClass().getDeclaredFields();
for (Field field : f) {
if (field.getName().equals("mYearPicker")|| field.getName().equals("mYearSpinner") ) {
field.setAccessible(true);
Object yearPicker = new Object();
yearPicker = field.get(picker1);
((View) yearPicker).setVisibility(View.GONE);
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
picker1.setCalendarViewShown(false);
builder1.setTitle("Please select date on which you would be leaving :")
.setView(picker1)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
SimpleDateFormat parseFormat = new SimpleDateFormat("EEE dd MMM");
Date date1=new Date();
date1.setDate(picker1.getDayOfMonth());
date1.setMonth(picker1.getMonth());
final String s =parseFormat.format(date1);
Log.e("DATE",s);
//Time picker
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
final TimePicker picker = new TimePicker(ctx);
picker.setIs24HourView(true);
builder.setTitle("Please select time at which you would be leaving :")
.setView(picker)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int hour=picker.getCurrentHour();
int minute=picker.getCurrentMinute();
str=s +" "+hour+":"+minute;
}
})
.setNegativeButton("One way", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}).create().show();
//Time picker
}
}
)
.setNegativeButton("One way",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
)
.create().show();
return "TIME "+str ;
}
}
以这种方式返回Time Null。请帮忙。谢谢
答案 0 :(得分:1)
你得到null因为dateTimePicker对话框在提供有效的日期时间之前需要用户交互,而你的函数会在显示选择器对话框后立即返回。
您可以实现以下内容:
/**
* Method Sets the date using Default Date Picker and fills it in EditText
*
* @param edittext
* @param context
* @param sharedprefs
*/
public void setDate(final EditText edittext) {
String date = edittext.getTag(R.id.tag_date_time).toString();
String[] splittedDate = date.split("-");
int year = Integer.parseInt(splittedDate[YEAR]);
int month = Integer.parseInt(splittedDate[MONTH]);
int dayOfMonth = Integer.parseInt(splittedDate[DATE]);
DatePickerDialog datedialog = new DatePickerDialog(mContext,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
edittext.setText(year + "-"
+ String.format("%02d", (monthOfYear + 1))
+ "-" + String.format("%02d", dayOfMonth));
edittext.setTag(R.id.tag_date_time, (year + "-"
+ String.format("%02d", (monthOfYear + 1))
+ "-" + String.format("%02d", dayOfMonth)));
}
}, year, (month - 1), dayOfMonth);
datedialog.show();
}
这里我们传递了需要设置日期的目标editText
。用户选择日期后,函数负责在edittext
中设置日期。类似的方法也可以与其他选择器(timePicker
,dateTimePicker
)和视图类型(textView
)一起使用。
答案 1 :(得分:1)
因为你做错了事。
CustomDateTimePicker
我希望它会帮助你。