我有一个打开对话框片段的活动。在该对话框片段中,用户输入一些文本。我希望在关闭对话框时将此输入文本返回到活动。任何建议将不胜感激。谢谢
调用DialogFrag的活动:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_task_fragment);
ActionBar ab = getActionBar();
ab.setDisplayHomeAsUpEnabled(true);
dte = (TextView) findViewById(R.id.datetv);
tme = (TextView) findViewById(R.id.timetv);
enteredTask = (EditText) findViewById(R.id.user_entered_task);
doit = (TextView) findViewById(R.id.title);
cal = (ImageButton) findViewById(R.id.select_date);
save = (Button) findViewById(R.id.save_tsk);
save.setOnClickListener(this);
cal.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view == cal) {
FragmentManager fm = getFragmentManager();
date_time_picker dTP = new date_time_picker();
dTP.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.myCustomDialog);
dTP.show(fm, "dTP");
Log.d("HirakDebug", "add_task_frag setDate called");
}
这是被调用的dialogFrag(这里用户选择日期和时间以及所有类型的东西)
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.date_time_picker, container, false);
timePicker = (TimePicker) v.findViewById(R.id.timePicker);
timetv = (TextView) v.findViewById(R.id.time);
datetv = (TextView) v.findViewById(R.id.date);
dateToday = (TextView) v.findViewById(R.id.date_today);
dateTomorrow = (TextView) v.findViewById(R.id.date_tomorrow);
datePick = (TextView) v.findViewById(R.id.pickDate);
set = (Button) v.findViewById(R.id.set);
set.setOnClickListener(this);
datePick.setOnClickListener(this);
dateToday.setOnClickListener(this);
dateTomorrow.setOnClickListener(this);
timePicker.setOnTimeChangedListener(this);
timePicker.setIs24HourView(false);
setCancelable(true);
return v;
}
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
Calendar calender = Calendar.getInstance();
calender.set(Calendar.HOUR_OF_DAY, hourOfDay);
calender.set(Calendar.MINUTE, minute);
timePicker.setIs24HourView(false);
SimpleDateFormat sdf = new SimpleDateFormat("hh : mm a");
timetv.setText("@ " + sdf.format(calender.getTime()));
}
@Override
public void onClick(View v) {
if (v == dateToday) {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EE, d MMM yyy");
datetv.setText("On " + sdf.format(c.getTime()));
}
else if (v == dateTomorrow) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, 1);
SimpleDateFormat sdf = new SimpleDateFormat("EE, d MMM yyy"); ;
String dateTomorrow = sdf.format(calendar.getTime());
datetv.setText("On " + dateTomorrow);
}
else if (v == datePick) {
setDate();
}
else if (v == set){
Log.d("HirakDebug", "dTP set button pressed");
String date = "123456";
Intent i = new Intent();
Bundle b = new Bundle();
String dateset = datetv.getText().toString();
String timeset = timetv.getText().toString();
b.putString("date", dateset);
b.putString("time", timeset);
i.putExtras(b);
getTargetFragment().onActivityResult(1, Activity.RESULT_OK, i);
dismiss();
}
}
public void setDate() {
new DatePickerDialog(getActivity(), d,
calender.get(Calendar.YEAR), calender.get(Calendar.MONTH),
calender.get(Calendar.DAY_OF_MONTH)).show();
Log.d("HirakDebug", "add_task_frag Date Picker showed");
}
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int year, int month, int day) {
calender.set(Calendar.YEAR, year);
calender.set(Calendar.MONTH, month);
calender.set(Calendar.DAY_OF_MONTH, day);
Log.d("HirakDebug", "add_task_frag Date was selected and updatedate called");
updatedate();
}
};
public void updatedate() {
SimpleDateFormat sdf = new SimpleDateFormat("EE, d MMM yyy");
datetv.setText("On " + sdf.format(calender.getTime()));
Log.d("HirakDebug", "add_task_frag Date was updated");
}
}
答案 0 :(得分:1)
你可以使用" SetTargeFragment"和" getTargetFragment"。请参阅此链接以获取更多信息http://developer.android.com/reference/android/app/Fragment.html
答案 1 :(得分:0)
您可以尝试使用侦听器接口。让您的活动实现界面并将其传递给对话框。只需谷歌类似" android监听器接口"。 Here解释了观察者模式。