Android日期选择器示例:DatePickerDialog.OnDateSetListener(){}必须实现继承的抽象方法onDateSet

时间:2010-08-17 11:37:57

标签: android

我正在浏览Android hello world应用程序示例并且已经停留在日期选择器示例上。我认为我已正确遵循说明但不理解以下错误

DatePickerDialog.OnDateSetListener(){}必须实现继承的抽象方法DatePickerDialog.OnDateSetListener.onDateSet

首先我输入了它,但现在我已经从示例中复制了代码:

// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }
        };

但仍然会收到错误。

很抱歉可能是新手类型错误,但我无法弄清楚这个错误试图告诉我什么或我可能做错了什么?

3 个答案:

答案 0 :(得分:2)

尝试:

public void onDateSet(android.widget.DatePicker view, int year,int monthOfYear, int dayOfMonth) 

答案 1 :(得分:2)

这是DatePickerDialog的一个实现。希望这会有所帮助。

private class ToDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    int year, month, day;
    DatePicker picker;

    public ToDatePickerFragment(int year, int month, int day){
        this.year = year;
        this.month = month;
        this.day = day;
    }

    protected DialogInterface.OnClickListener btn_cancel_listener = new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();               
        }
    };

    protected DialogInterface.OnClickListener btn_set_listener = new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            picker.clearFocus();
            onDateSet(picker, picker.getYear(), picker.getMonth(), picker.getDayOfMonth());
        }
    };

    protected DialogInterface.OnClickListener btn_apply_listener = new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            picker.clearFocus();
            onDateSet(picker, picker.getYear(), picker.getMonth(), picker.getDayOfMonth());

            SearchAsyncTask searchAsyncTask = new SearchAsyncTask();
            searchAsyncTask.execute();
        }
    };

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), null, this.year, this.month, this.day);
        datePickerDialog.setCancelable(true);
        datePickerDialog.setCanceledOnTouchOutside(true);
        datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", btn_cancel_listener);

        picker = datePickerDialog.getDatePicker();

        datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Set", btn_set_listener);

        datePickerDialog.setButton3("Apply now", btn_apply_listener);

        datePickerDialog.setTitle("Set to date:");
        return datePickerDialog;
    }



    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        String sToDate = createStringFromDateElements(year, monthOfYear, dayOfMonth);
        References.orderlist_filters.setDateto(sToDate);
        Toast.makeText(getActivity(), sToDate, Toast.LENGTH_SHORT).show();
    }
}

答案 2 :(得分:1)

您必须添加导入:

import android.widget.DatePicker;