public class MainFragment extends Fragment {
private static final String ARG_DATE="com.example.ting.datepickerfragment,date";
private static final String TAG_DATE="DatePickerFragment";
private static final int REQUEST_CODE=0;
private Button mButton;
private Date mDate;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater,container,savedInstanceState);
View v=inflater.inflate(R.layout.buttonview,container,false);
mButton= (Button) v.findViewById(R.id.button_view);
mDate= (Date) getArguments().getSerializable(ARG_DATE);
mButton.setText(getArguments().getSerializable(ARG_DATE).toString()+"");
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(getActivity(),"请不要点我!",Toast.LENGTH_SHORT).show();
// DialogFragment.
FragmentManager fm=getFragmentManager()//获取目前的Fragment,由于是在Fragment可以直接获取,如果在activity中需要自己创建。
DatePickerFragment datePickerFragment=DatePickerFragment.newInstance(mDate);
datePickerFragment.setTargetFragment(MainFragment.this,REQUEST_CODE);//设置将要被启动Fragment的父Fragment.
datePickerFragment.show(fm,TAG_DATE);//启动DialogFragment
}
});
// mButton.setText("sdfsdfsdf");
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mDate= (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
mButton.setText(mDate.toString());
}
public static MainFragment newInstance(Date date){
Bundle bundle=new Bundle();//将当前数据绑定到Bundle,通过Arguments传入Fragment
bundle.putSerializable(ARG_DATE,date);
MainFragment fragment=new MainFragment();
fragment.setArguments(bundle);
return fragment;
}
}
/****************************************************/
public class DatePickerFragment extends DialogFragment {
private static final String BUNDLE_DATE = "com.example.ting.datepickerfragment.date";
public static final String EXTRA_DATE="com.example.ting.datepickerfragment.date";
private DatePicker mDatePicker;
private Date date;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {//返回对话框样式
super.onCreateDialog(savedInstanceState);
View v = LayoutInflater.from(getActivity()).inflate(R.layout.date_picker, null);
mDatePicker = (DatePicker) v.findViewById(R.id.date_picker_view);
date = (Date) getArguments().getSerializable(BUNDLE_DATE);
mDatePicker.init(date.getYear(), date.getMonth(), date.getDay(), null);
//创建AlertDialog样式。
return new AlertDialog.Builder(getActivity()).setTitle("Welcome").setView(v).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Date mDate= new GregorianCalendar(mDatePicker.getYear(),mDatePicker.getMonth(),mDatePicker.getDayOfMonth()).getTime();
Intent intent=new Intent();
intent.putExtra(EXTRA_DATE,mDate);
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK,intent);
//通过直接调用父Fragment的onActivityResult进行值传递。
//父Fragment通过实现onActivityResult方法来接收数据。
}
}).create();
}
public static DatePickerFragment newInstance(Date date) {
Bundle bundle = new Bundle();//将值绑定Bundle 通过Arguments传入子Fragment
bundle.putSerializable(BUNDLE_DATE, date);
DatePickerFragment datePickerFragment = new DatePickerFragment();
datePickerFragment.setArguments(bundle);
return datePickerFragment;
}
}