从Android内部的DialogFragment中获取日期和时间值

时间:2017-03-07 19:14:58

标签: android android-fragments datepicker fragment timepicker

您试图从各自的类中获取日期和时间的值,并将它们设置在片段中的文本视图中。 我已经尝试在变量中设置它并从方法调用它但它似乎不起作用

这是片段类中的代码,我想设置textviews set_date和set_time

Private

SelectDateFragment Class

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_edit_timetable, container, false);

    selectDate = (Button) view.findViewById(R.id.selectDate);
    selectTime = (Button) view.findViewById(R.id.selectTime);
    set_date = (TextView) view.findViewById(R.id.set_date);
    set_time = (TextView) view.findViewById(R.id.set_time);
    selectDate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // Show Date dialog
            DialogFragment newFragment = new SelectDateFragment();
            newFragment.show(getActivity().getFragmentManager(), "Date Picker");

        }
    });
    selectTime.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // Show time dialog
            DialogFragment newFragment  = new SelectTimeFragment();
            //newFragment.show(getActivity().getFragmentManager(), DIALOG_TIME);
            newFragment.show(getActivity().getFragmentManager(), "Time Picker");
        }
    });

    return view;
}

SelectTimeFragment类

public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

private String date = "";
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar calendar = Calendar.getInstance();
    int yy = calendar.get(Calendar.YEAR);
    int mm = calendar.get(Calendar.MONTH);
    int dd = calendar.get(Calendar.DAY_OF_MONTH);
    return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}

@Override
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
    populateSetDate(yy, mm+1, dd);
}
public void populateSetDate(int year, int month, int day) {
    date = month+"/"+day+"/"+year;
}

public String getDate() {
    return date;
}

2 个答案:

答案 0 :(得分:0)

在选择[日期/时间]片段中添加此方法。 private void sendResult(int resultCode,Date mDate){     if(getTargetFragment()== null)         返回;     意图i =新的Intent();     i.putExtra(" EXTRA_DATE",mDate);     getTargetFragment()。onActivityResult(getTargetRequestCode(),resultCode,i); } 在populateSetDate()中添加此行 public void populateSetDate(int year,int month,int day){     date = month +" /" + day +" /" + year;     sendResult(Activity.RESULT_OK,date); //添加这个 } 在dialogFragment实例下添加此行 selectDate.setOnClickListener(new OnClickListener(){     @覆盖     public void onClick(查看arg0){         DialogFragment newFragment = new SelectDateFragment();         newFragment.setTargetFragment(TIMETABLE_FRAGMENT,REQUEST_DATE); //添加这个         newFragment.show(getActivity()。getFragmentManager()," Date Picker");     } }); 最后在TIMETABLE_FRAGMENT中添加此方法 @覆盖 public void onActivityResult(int requestCode,int resultCode,Intent data){     if(resultCode!= Activity.RESULT_OK)         返回;     if(requestCode == REQUEST_DATE){         date =(Date)data.getSerializableExtra(" EXTRA_DATE"); //这是你选择的日期     } }

答案 1 :(得分:0)

假设您在onDateSet获得了预期的数据,请尝试以下操作:

implements移除SelectDateFragment并删除onDateSet。更改您致电DatePickerDialog的方式。

public class SelectDateFragment extends DialogFragment {

private String date = "";

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar calendar = Calendar.getInstance();
    int yy = calendar.get(Calendar.YEAR);
    int mm = calendar.get(Calendar.MONTH);
    int dd = calendar.get(Calendar.DAY_OF_MONTH);

    return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getParentFragment(), yy, mm, dd);
}

public void populateSetDate(int year, int month, int day) {
    date = month+"/"+day+"/"+year;
}

public String getDate() {
    return date;
}

implements添加到顶级片段。您可以在此处设置日期和时间。另外,在onDateSet添加:

Public class YourTopFragment extends Fragment implements DatePickerDialog.OnDateSetListener

...

@Override
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
    set_date.setText(...the text you want to set goes here...);
}

这将指示DialogFragment直接通知您的顶级片段所选日期。

类似的东西应该用于设定时间。