How to set the value of the datePicker in to EditText?

一个人想着一个人 提交于 2019-12-06 05:36:41

What you need to do is get a reference to the EditText and the DatePicker:

EditText editText = (EditText) findViewById( R.id.dateSelectionEditText );
DatePicker datePicker = (DatePicker) findViewById( R.id.datePicker );

Then you need to create an OnDateChangedListener:

private class MyOnDateChangedListener implements OnDateChangedListener {
  @Override
  public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
     editText.setText( "" + dayOfMonth + "-" + (monthOfYear + 1) + "-" + year );
  }
};

All thats left to do is just initialize the DatePicker:

datePicker.init( year, monthOfYear, dayOfMonth, new MyOnDateChangedListener() ).

Check this out Date pick tutorial

It shows you the best way to implement this. Then you can just change it to your liking.

set like this

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

        selectedDate = Integer.toString(mMonth)+"/"+Integer.toString(mDay)+"/"+Integer.toString(mYear);
        date_text.setText(selectedDate);
        System.out.println("selected date is:"+selectedDate);
        edittext.setText(selectedDate );
    }
};
Karthi

Initialize these variable globally in your activity ,

private EditText zopenDate;
        Button calendar;

        private int mYear;
        private int mMonth;
        private int mDay;
        private DatePickerDialog.OnDateSetListener mDateSetListener;

inside your instance

calendar=(Button)findViewById(R.id.datePicker);
zopenDate=(EditText)findViewById(R.id.dateSelectionEditText);

inside your instance use this listeners,

mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

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



calendar.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    showDialog(DATE_DIALOG_ID);
                }
            });




final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

            // display the current date (this method is below)
            updateDisplay();

the function updatedisplay() outside createInstance(),

private void updateDisplay() {
        zopenDate.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append(" "));
    }

Use this to reflect date in edit text

private OnDateSetListener setDOB = new OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int month,
                    int dayOfMonth) {
                datePicker = (EditText) findViewById(R.id.dateOB);
                datePicker.setText(" " + dayOfMonth + "/" + (month + 1) + "/"
                        + year + "");

            }
        };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!