从一个片段我调用一个对话框
private void showDateDialog(){
FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = new DatePickerDialogFragment((Fragment)this,localCalendar);
newFragment.show(ft, "dialog");
}
然后在自定义对话框中我调用 timesetlistener
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
final View v = inflater.inflate(R.layout.time_dialog, null);
timePicker=(TimePicker)v.findViewById(R.id.timePicker1);
timePicker.setIs24HourView(true);
if (data!=null) {
timePicker.setCurrentHour(data.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(data.get(Calendar.MINUTE));
}
timePicker.setScaleX(2);
timePicker.setScaleY(2);
return new TimePickerDialog.Builder(getActivity())
.setTitle(R.string.timeDialog1)
.setView(v)
.setCancelable(true)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//validation code
OnTimeSetListener otsl=(OnTimeSetListener)mFragment;
otsl.onTimeSet(timePicker, timePicker.getCurrentHour(), timePicker.getCurrentMinute());
}
})
.setNegativeButton(R.string.annulla, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
}
在片段中我实现了一个 OnTimeSetListener 来获取值
public void onTimeSet(TimePicker view, int hour, int minute) {
localCalendar.set(Calendar.HOUR_OF_DAY, hour);
localCalendar.set(Calendar.MINUTE, minute);
updateTime(hour,minute);
}
这工作正常。但我希望另一个自定义对话框返回一个字符串,但我不知道要实现哪个监听器。我尝试了“onDismiss”,但无法传递参数,我尝试了 OnValueChangeListener,但由于参数有 2 个整数(我需要一个字符串!)。这是正确的方法吗?我的意思是,使用听众。
好消息是,从任何实现 onDateChange 的片段中,我都可以启动对话框并检索值。