本文介绍了日期选择器示值误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个日期选取器添加到我的code,但正在显示一个错误了pickerListener里面的fromDate_textView.setOnClickListener说:

任何人都可以请帮我解决这个问题?

  fromDate_textView.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            的ShowDialog(DatePickerDialog(Sell_Product_Activity.this,
                    pickerListener,年,月,日));
        }
    });

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

        //当关闭对话框,如下方法将被调用。
        @覆盖
        公共无效onDateSet(DatePicker的观点,诠释selectedYear,
                INT selectedMonth,诠释selectedDay){

            年= selectedYear;
            月= selectedMonth;
            天= selectedDay;

            //显示选定的日期
            fromDate_textView.setText(新的StringBuilder()。追加(月+ 1)
                    .append( - )。追加(天).append( - )。追加(年)
                    。附加( ));

        }
    };
 

解决方案

使用这种code可以解决你的问题,它的做工精细

  //日期选取器

        私人诠释一年;
        私人诠释一个月;
        私人诠释天;
        静态最终诠释DATE_PICKER_ID = 1111;

//为日期选择器
    私人的EditText m3_DateDisplay;

    @覆盖
        保护无效的onCreate(包savedInstanceState)
    {

        super.onCreate(savedInstanceState);

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


         //获取当前日期日历

        最后的日历C = Calendar.getInstance();
        年= c.get(Calendar.YEAR);
        月= c.get(Calendar.MONTH);
        天= c.get(Calendar.DAY_OF_MONTH);

        //显示当前的日期

        /*m3_DateDisplay.setText(new的StringBuilder()
                //月第一个值为0,只需添加1
                .append(天).append( - )。追加(月+ 1).append( - )
                .append(年).append()); * /


        //显示选定的日期
        StringBuilder的dateValue1 =新的StringBuilder()追加(天).append( - )。
        。.append(月+ 1).append( - )追加(年).append();


        //转换为正确的日期格式保存到数据库
        SimpleDateFormat的sdf123 =新的SimpleDateFormat(DD-MM-YYYY);
        串ABS1 = dateValue1.toString();
        日期testDate1 = NULL;
         尝试 {
            testDate1 = sdf123.parse(ABS1);
        }赶上(ParseException的E){

            e.printStackTrace();
        }
        SimpleDateFormat的formatter1 =新的SimpleDateFormat(DD-MM-YYYY);
        日期格式字符串= formatter1.format(testDate1);

        m3_DateDisplay.setText(日期格式);



            m3_DateDisplay.setFocusable(假);
            m3_DateDisplay.setInputType(InputType.TYPE_NULL);
            m3_DateDisplay.setOnClickListener(新View.OnClickListener(){
                @燮pressWarnings(德precation)
                @覆盖
                公共无效的onClick(视图v){
                    的ShowDialog(DATE_PICKER_ID);
                }
            });

    }

    @覆盖
        受保护的对话框onCreateDialog(INT ID)
        {
            开关(ID){
            案例DATE_PICKER_ID:

                //打开日期选择器对话框。
                //设置日期选取器的当前日期
                //添加pickerListener听者到日期选择器
                //返回新DatePickerDialog(这一点,pickerListener,年,月,日);

                /////只显示到日期不多说。
                    DatePickerDialog对话框=新DatePickerDialog(这一点,pickerListener,年,月,日);
                    。dialog.getDatePicker()setMaxDate(新日期()的getTime());
                    返回对话框;
            }
            返回null;
        }

        私人DatePickerDialog.OnDateSetListener pickerListener =新DatePickerDialog.OnDateSetListener()
        {

            //当关闭对话框,如下方法将被调用。
            @覆盖
            公共无效onDateSet(DatePicker的观点,诠释selectedYear,
                    INT selectedMonth,诠释selectedDay){

                年= selectedYear;
                月= selectedMonth;
                天= selectedDay;



                //显示选定的日期
                            StringBuilder的DATEVALUE =新的StringBuilder()追加(天).append( - )。
                            。.append(月+ 1).append( - )追加(年).append();


                //转换为正确的日期格式保存到数据库
                    SimpleDateFormat的sdf123 =新的SimpleDateFormat(DD-MM-YYYY);
                            串ABS1 = dateValue.toString();
                            日期testDate1 = NULL;
                             尝试 {
                                testDate1 = sdf123.parse(ABS1);
                            }赶上(ParseException的E){

                                e.printStackTrace();
                            }
                SimpleDateFormat的formatter1 =新的SimpleDateFormat(DD-MM-YYYY);
                            日期格式字符串= formatter1.format(testDate1);

                            m3_DateDisplay.setText(日期格式);


            }
        };
 

I'm trying to add a Date Picker to my code, but an error is being shown over "pickerListener" inside the "fromDate_textView.setOnClickListener" saying:

Can anyone please help me solve the issue?

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(" "));

        }
    };
解决方案

Use this Code may solve your Problem, Its working fine

  // 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);


            }
        };

这篇关于日期选择器示值误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 20:35