我已经在我的应用中创建了自定义date and time picker
,现在我已多次使用picker
。因此,为了避免代码的冗余,我认为我应该将该函数放在另一个类的单独函数中,我可以在每次需要时调用它,但我无法这样做,请帮助。
单独的类是:
public class DateTimePicker {
String str;
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 "hello "+ str ;
}
以及我使用上述类的代码是:
DateTimePicker obj = new DateTimePicker();
Log.e("Object", "Created");
String st = obj.returnDate(this.getActivity());
Log.e("Function", "Called");
System.out.println("Value returned :" + st);
leaving.setText(st);
在日志中我得到" hello null "一旦我调用了它甚至不等待用户输入的功能,evething就会被打印出来。 请帮忙谢谢
答案 0 :(得分:0)
如果您调用show(),代码将不会停止并等待用户输入。尝试添加:
[...]
}).create().show();
Log.i("Dialog", "Show called");
了解会发生什么。
如果你想使用Dialog中的输入数据,你必须从正面按钮监听器中调用一个函数:
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int hour=picker.getCurrentHour();
int minute=picker.getCurrentMinute();
sendToSomewhere("+hour+":"+minute");
}
})
答案 1 :(得分:0)
您将始终返回“hello null”,因为该对话框在内部创建了一个新线程,并且该对话框的答案仅在相应的按钮单击侦听器中可用。
您可以做的是为创建对话框的函数提供callback
方法,并在对话框中得到答案后调用此方法。
有关如何将函数作为参数传递的详细信息,请参阅Java Pass Method as Parameter。
修改强>
第二个想法是,将onClickListeners
作为对话函数的参数发送会更优雅。
您可以为流程中的所有三个最后一个按钮定义onclick,如
DialogInterface.OnClickListener
yesButton = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int hour = picker.getCurrentHour();
int minute = picker.getCurrentMinute();
str = s + " " + hour + ":" + minute;
SomeFunction();
}
});
将这些对象中的3个传递给对话框函数,而不需要像
这样的函数头public void returnDate(final Context ctx, OnClickListener yes, OnClickListener no1, OnClickListener no2) {
答案 2 :(得分:0)
基本上你应该定义返回void的方法。要接受用户输入String,您可以使用回调。
DateTimePicker
类中定义界面。您的DateTimePicker
课程如下:
public class DateTimePicker {
private final OnUserInputListener onUserInputListener;
// String str; <-- this field value has no use
DateTimePicker(OnUserInputListener onUserInputListener) {
this.onUserInputListener = onUserInputListener;
}
public void returnDate(final Context ctx) {
...
public void onClick(DialogInterface dialog, int whichButton) {
int hour = picker.getCurrentHour();
int minute = picker.getCurrentMinute();
str = s + " " + hour + ":" + minute;
onUserInputListener.onUserInput(str); // CALL BACK!!!
}
...
}
public interface OnUserInputListener {
void onUserInput(String string);
}
}
对于调用者类,它应该实现OnUserInputListener。
public class MainActivity extends Activity implements OnUserInputListener {
...
DateTimePicker obj = new DateTimePicker(this);
obj.returnDate(this.getActivity());
...
@override
public void onUserInput(String string) {
System.out.println("Value returned :" + string);
leaving.setText(string);
}
}
答案 3 :(得分:0)
这可能会对你有帮助!
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
TextView textView, tv;
public DatePickerFragment(TextView textView, TextView text) {
this.textView = textView;
tv = text;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
mCalendar.set(Calendar.DAY_OF_MONTH, day);
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
String dateForButton = dateFormat.format(mCalendar.getTime());
textView.setText(dateForButton);
tv.setVisibility(View.VISIBLE);
}
}
和show dialog
dateFragment = new DatePickerFragment(toDate, tvTo);
dateFragment.show(getFragmentManager(), "Dialog Title");
您可以在活动/自定义类
中使用它答案 4 :(得分:0)
尝试在DateTimePicker中添加自定义日期选择的侦听器:
public interface OnDateSelectedListener{
public void onDateSelected(String date);
}
在DateTimePicker中添加自定义接口并设置侦听器引用returnDate:
public class DateTimePicker {
public void returnDate(final Context ctx, final OnDateSelectedListener onDateSelectedListener) {
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();
onDateSelectedListener.onDateSelected(s + " " + hour + ":" + minute);
}
})
.setNegativeButton("One way", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}).create().show();
}
}
)
.setNegativeButton("One way", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
})
.create().show();
}
public interface OnDateSelectedListener{
public void onDateSelected(String date);
}
}
现在使用OnDateSelectedListener引用调用DateTimePicker:
DateTimePicker dateTimePicker = new DateTimePicker();
dateTimePicker.returnDate(this.getActivity(), new DateTimePicker.OnDateSelectedListener() {
@Override
public void onDateSelected(String date) {
leaving.setText(st);
}
});