自定义的DatePicker上的EditText

自定义的DatePicker上的EditText

本文介绍了自定义的DatePicker上的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2 的EditText

当有人点击一个的EditText 日期选择器显示设置于的EditText 日期。所以我的编码的EditText

When anyone click on a EditText datepicker is displayed to set date in EditText. so my coding for EditText is

    sdate.setOnTouchListener( new DrawableClickListener.RightDrawableClickListener(sdate)
                {
                    @Override
                    public boolean onDrawableClick()
                    {
                         MyDatePickerDialog(sdate);// put edit text view.
                          return true;
                    }
                } );
                edate.setOnTouchListener( new DrawableClickListener.RightDrawableClickListener(edate)
                {
                    @Override
                    public boolean onDrawableClick()
                    {
                         MyDatePickerDialog(edate);// put edit text view.
                         return true;
                    }
                } );

和我自定义的日期选择器是

And my custom datepicker is

    private void MyDatePickerDialog(final EditText sdate) {
        Toast.makeText(getApplicationContext(),"sdgfdg" , Toast.LENGTH_LONG).show();
        DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }

            private void updateLabel() {
                 String myFormat = "MM/dd/yy"; //In which you need put here
                    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                    sdate.setText(sdf.format(myCalendar.getTime()));
            }

        };

    }

但问题是,显示敬酒消息,但它dosent显示日期选择器

But the problem is toast message is displayed but it dosent display datepicker

推荐答案

您只有一个自定义的方法,你需要创建一个 OnDateSetListener 的监听器来传递你的 DatePickerDialog

You have a custom method only and you need to create a listener for OnDateSetListener to pass your DatePickerDialog

 private void MyDatePickerDialog(final EditText sdate) {
    Toast.makeText(getApplicationContext(),"sdgfdg" , Toast.LENGTH_LONG).show();
     Calendar newCalendar = Calendar.getInstance();
     DatePickerDialog myPickerDialog = new DatePickerDialog(this, new OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            updateLabel();
        }

    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

        private void updateLabel() {
             String myFormat = "MM/dd/yy"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                sdate.setText(sdf.format(newCalendar.getTime()));
        }

    myPickerDialog.show();

  }

注::您可以使用 onClickListener 而不是 onTouchListener

这篇关于自定义的DatePicker上的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:02