作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
我有一个 MainFragment
,其中一个按钮创建了一个 AlertDialog
,其中有多个字段,其中之一是日期,在日期按钮中我调用了一个 DatePickerDialog
并选择日期,所以我的问题是:
如何检索该日期并将其设置在 MainFragment 上警报对话框的
?TextView
上
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);
}
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);
}
}
public class MainActivty implements CalendarPickerFragment.OnDateReceiveCallBack{
@Override
public void onDateReceive(int dd, int mm, int yy) {
MainFragment.displaydate(dd,mm,yy);
}
}
我是一名优秀的程序员,十分优秀!