【发布时间】:2016-08-31 16:17:11
【问题描述】:
如何获取从DatePickerFragment 中选择的日期?
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
int Year,Month,Day;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
}
我的Fragment 我想在其中获取日期并将其设置为文本字段:
public class CalculatorHomeFragment extends Fragment
LinearLayout lyDate;
TextView lblDate;
int Year,Month,Day;
private static CalculatorHomeFragment instance = null;
public CalculatorHomeFragment() {
}
public static CalculatorHomeFragment getInstance() {
if (instance == null)
instance = new CalculatorHomeFragment();
return instance;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calculator_home, container, false);
lyDate = (LinearLayout) rootView.findViewById(R.id.lyDate);
lblDate = (TextView) rootView.findViewById(R.id.lblDate);
lyDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getActivity().getFragmentManager(), "datePicker");
}
});
return rootView;
}
【问题讨论】:
-
我在 DatePickerFragment 的 onDateSet 方法中获取日期,但我希望它在 CalculatorHomeFragment 的主要片段中。
-
你需要添加activity和fragment之间的通信来更新值。
标签: android android-fragments datepicker