日期选择器显示错误

时间:2015-05-25 20:02:34

标签: android eclipse datepicker

我试图在我的代码中添加日期选择器,但是错误显示在" pickerListener"在" fromDate_textView.setOnClickListener"话说:

  

pickerListener无法解析为变量

有谁可以帮我解决这个问题?

fromDate_textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(DatePickerDialog(Sell_Product_Activity.this,
                    pickerListener, year, month, day));
        }
    });

    DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // Show selected date
            fromDate_textView.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));

        }
    };

4 个答案:

答案 0 :(得分:1)

使用此代码可以解决您的问题,其工作正常

  // for date picker

        private int year;
        private int month;
        private int day;
        static final int DATE_PICKER_ID = 1111;

// for date picker
    private EditText m3_DateDisplay;

    @Override
        protected void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);

m3_DateDisplay = (EditText) findViewById(R.id.editText3);


         // Get current date by calender

        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);

        // Show current date

        /*m3_DateDisplay.setText(new StringBuilder()
                // Month is 0 based, just add 1
                .append(day).append("-").append(month + 1).append("-")
                .append(year).append(" "));*/


        // Show selected date
        StringBuilder dateValue1=new StringBuilder().append(day).append("-")
        .append(month + 1).append("-").append(year).append(" ");


        //for Converting Correct Date format Save into Database
        SimpleDateFormat sdf123 = new SimpleDateFormat("dd-MM-yyyy");
        String abs1= dateValue1.toString();
        Date testDate1 = null;
         try {
            testDate1 = sdf123.parse(abs1);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        SimpleDateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy");
        String DateFormat=formatter1.format(testDate1);

        m3_DateDisplay.setText(DateFormat);



            m3_DateDisplay.setFocusable(false);
            m3_DateDisplay.setInputType(InputType.TYPE_NULL);
            m3_DateDisplay.setOnClickListener(new View.OnClickListener() {
                @SuppressWarnings("deprecation")
                @Override
                public void onClick(View v) {
                    showDialog(DATE_PICKER_ID);
                }
            });

    }

    @Override
        protected Dialog onCreateDialog(int id)
        {
            switch (id) {
            case DATE_PICKER_ID:

                // open datepicker dialog.
                // set date picker for current date
                // add pickerListener listner to date picker
                //return new DatePickerDialog(this, pickerListener, year, month, day);

                /////Only Show till Date Not More than That.
                    DatePickerDialog dialog = new DatePickerDialog(this, pickerListener, year, month, day);
                    dialog.getDatePicker().setMaxDate(new Date().getTime());
                    return dialog;
            }
            return null;
        }

        private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener()
        {

            // when dialog box is closed, below method will be called.
            @Override
            public void onDateSet(DatePicker view, int selectedYear,
                    int selectedMonth, int selectedDay) {

                year = selectedYear;
                month = selectedMonth;
                day = selectedDay;



                // Show selected date
                            StringBuilder dateValue=new StringBuilder().append(day).append("-")
                            .append(month + 1).append("-").append(year).append(" ");


                //for Converting Correct Date format Save into Database
                    SimpleDateFormat sdf123 = new SimpleDateFormat("dd-MM-yyyy");
                            String abs1= dateValue.toString();
                            Date testDate1 = null;
                             try {
                                testDate1 = sdf123.parse(abs1);
                            } catch (ParseException e) {

                                e.printStackTrace();
                            }
                SimpleDateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy");
                            String DateFormat=formatter1.format(testDate1);

                            m3_DateDisplay.setText(DateFormat);


            }
        };

答案 1 :(得分:0)

您必须在设置OnClickListener之前声明pickerListener并使pickerListener成为final,以便可以在OnClickListener中访问它。这应该有效:

final DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    @Override
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {

        year = selectedYear;
        month = selectedMonth;
        day = selectedDay;

        // Show selected date
        fromDate_textView.setText(new StringBuilder().append(month + 1)
                .append("-").append(day).append("-").append(year)
                .append(" "));

    }
};

fromDate_textView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        DatePickerDialog datePickerDialog= new DatePickerDialog(Sell_Product_Activity.this,
        pickerListener, year, month, day);
        datePickerDialog.show();
    }
});

修改: 我将我的第一个答案与Sanjeet Ajnabee的答案结合起来。我希望这个对你有用!我测试时没有问题。

答案 2 :(得分:0)

修改代码 -

fromDate_textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(DatePickerDialog(Sell_Product_Activity.this,
                    pickerListener, year, month, day));
        }
    });

AS -

fromDate_textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatePickerDialog datePickerDialog= new DatePickerDialog(this,
            pickerListener, year, month, day);
            datePickerDialog.show();
        }
    });

答案 3 :(得分:0)

感谢您的回答。问题实际上是我在项目中使用的“Monkey Talk”库。删除此lib后,日期选择器开始正常工作