您试图从各自的类中获取日期和时间的值,并将它们设置在片段中的文本视图中。 我已经尝试在变量中设置它并从方法调用它但它似乎不起作用
这是片段类中的代码,我想设置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;
}
答案 0 :(得分:0)
答案 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
直接通知您的顶级片段所选日期。
类似的东西应该用于设定时间。