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(),..」? –