gpt4 book ai didi

android - 从对话框 fragment 返回一些东西

转载 作者:行者123 更新时间:2023-11-30 02:35:17 25 4
gpt4 key购买 nike

我有一个打开对话框 fragment 的 Activity 。在该对话 fragment 中,用户输入了一些文本。当对话框关闭时,我想将此输入文本接收回 Activity 。任何建议将不胜感激。谢谢你

调用 DialogFrag 的 Activity :

@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");
}

}

最佳答案

您可以使用“SetTargeFragment”和“getTargetFragment”。有关更多信息,请参阅此链接 http://developer.android.com/reference/android/app/Fragment.html

关于android - 从对话框 fragment 返回一些东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26672561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com