0
嗨,我是新的android开发。我遇到了问题。 我有一个片段。在片段视图中,我得到了一个DatePicker的图像按钮。当我运行代码时没有发现错误。但日期选取对话框不显示当我点击图像按钮..DatePicker里面的片段
任何人请帮助我这一点。
这里是代码:
public class Payment05TowMachineEntryFragment extends Fragment {
public static MainActivity lastCreated;
private TextView DateDisplay;
private ImageButton PickDate;
private Calendar Date;
static final int DATE_DIALOG_ID = 999;
private TextView activeDateDisplay;
private Calendar activeDate;
public Payment05TowMachineEntryFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.payment05_towmachine_entry_fragment, container, false);
return rootView;
}
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DateDisplay = (TextView) (getView().findViewById(R.id.selectDate));
PickDate = (ImageButton) (getView().findViewById(R.id.btncalender));
/* get the current date */
Date = Calendar.getInstance();
/* add a click listener to the button */
PickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDateDialog(DateDisplay, Date);
}
});
/* display the current date (this method is below) */
updateDisplay(DateDisplay, Date);
}
private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
activeDate.set(Calendar.YEAR, year);
activeDate.set(Calendar.MONTH, monthOfYear);
activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(activeDateDisplay, activeDate);
//unregisterDateDisplay();
}
};
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(getActivity(), dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
}
return null;
}
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
break;
}
}
private void showDateDialog(TextView dateDisplay, Calendar date) {
activeDateDisplay = dateDisplay;
activeDate = date;
showDialog(DATE_DIALOG_ID);
}
private void showDialog(int dateDialogId) {
}
private void updateDisplay(TextView dateDisplay, Calendar date) {
DateDisplay.setText(
new StringBuilder()
.append(date.get(Calendar.DAY_OF_MONTH)).append("-")
.append(date.get(Calendar.MONTH) + 1).append("-")
.append(date.get(Calendar.YEAR)).append(" "));
}
}
嗨@gargs为什么我的错误“show(getFragmentManager(),..”? –