gpt4 book ai didi

android - 如何从 DatePickerDialog 检索数据到 AlertDialog

转载 作者:行者123 更新时间:2023-11-29 18:54:54 27 4
gpt4 key购买 nike

<分区>

我有一个 MainFragment,其中一个按钮创建了一个 AlertDialog,其中有多个字段,其中之一是日期,在日期按钮中我调用了一个 DatePickerDialog 并选择日期,所以我的问题是:
如何检索该日期并将其设置在 MainFragment 上警报对话框的 TextView?

这是在主要 fragment 的 AlertDialog 上:

public class FragmentInicio extends Fragment{
String date;

public void addDate(){

View view = (LayoutInflater.from(getActivity())).inflate(R.layout.alert_dialog,null);

AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setView(view);

final EditText date_EditText = view.findViewById(R.id.dateEditText);
final ImageButton dateButton= view.findViewById(R.id.tareaFechaButton);
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CalendarPickerFragment newFragment = new CalendarPickerFragment();
newFragment.show(getFragmentManager(), "DatePicker");

}
});

date_EditText.setText(date);

alertDialog.setTitle("New Date");
alertDialog.setCancelable(true).setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Some stuff
}
});

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
Dialog dialog = alertDialog.create();
dialog.show();

}

public static void displaydate(int year, int monthOfYear, int dayOfMonth) {
date = String.format("%02d", dayOfMonth) + "/" + String.format("%02d", monthOfYear + 1) + "/" +
String.format("%02d", year);
Log.d("DTAG", "date: "+date);
}

这是在 CalendarPickerFragment 上

Update

public class CalendarPickerFragment extends DialogFragment implements  DatePickerDialog.OnDateSetListener {

private CalendarPickerFragment.OnDateReceiveCallBack mListener;
private Context context;

public interface OnDateReceiveCallBack {
void onDateReceive(int dd ,int mm, int yy);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;

try {
mListener = (CalendarPickerFragment.OnDateReceiveCallBack) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnDateSetListener");
}
}

public CalendarPickerFragment(){
}



@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

final Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);

return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Log.d(TAG, i + "/" + "/" + i1 + "/" + i2);
mListener.onDateReceive(i,i1,i2);
}
}

主 Activity

public class MainActivty implements CalendarPickerFragment.OnDateReceiveCallBack{
@Override
public void onDateReceive(int dd, int mm, int yy) {
MainFragment.displaydate(dd,mm,yy);
}
}

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