我从onClick()调用了三个方法。
我想要的是当我点击这个片段中的按钮(即NeedBloodFragment)时,会出现日期和时间选择器,然后打开另一个片段。
截至目前,我在此片段以及其他片段中将日期和时间的值视为空白。问题很可能是这三种方法以一种奇怪的方式被调用。我无法弄清楚如何正确地做到这一点。
代码的基本部分是:
public class NeedBloodFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_need_blood, container, false);
Button button1 = (Button)rootView.findViewById(R.id.dialog_btn1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
onCreateDialog(999).show();
onCreateDialog(1999).show();
openAppointments();
}
});
return rootView;
}
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (id == 999) {
DatePickerDialog d= new DatePickerDialog(getActivity(), myDateListener, 2015, 6, 25);
d.setTitle("Select Meeting Date");
return d;
}
if(id == 1999) {
TimePickerDialog t=new TimePickerDialog(getActivity(),
myTimeListener,
Calendar.HOUR_OF_DAY,
Calendar.MINUTE,
true);
t.setTitle("Select Meeting Time");
return t;
}
return null;
}
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
// arg1 = year
// arg2 = month
// arg3 = day
StringBuilder sb=new StringBuilder();
sb.append(arg3).append("/")
.append(arg2+1).append("/").append(arg1);
returnDate(sb.toString());
}
};
private TimePickerDialog.OnTimeSetListener myTimeListener=new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
StringBuilder sb=new StringBuilder();
sb.append(hourOfDay).append(":").append(minute);
returnTime(sb.toString());
}
};
public void returnDate(String s)
{
MainActivity.selectedDate=s;
}
public void returnTime(String s)
{
MainActivity.selectedTime=s;
}
public void openAppointments()
{
fragment = new AppointmentsFragment();
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).addToBackStack( null ).commit();
}
else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
//dialog.dismiss();
}
}
准确地说,这些是我的问题:
selectedDate
和selectedTime
没有任何价值观,我该如何纠正?