简体   繁体   中英

Showing data using DatePickerDialog on EditText

I was trying to make an EditText where when I click on it a DatePickerDialog will show up so I can choose my birthday and then the date I choose will be written in the EditText . I found this code online but I am having problems with it as the line where it says:

new DatePickerDialog(TheClassName.this, datePickerListener, myCalendar..

The datePickerListener is colored red and I don't understand why? If someone can help me I would really appreciate it.

//some of the needed imports
import android.widget.DatePicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;


//private field variables in the Activity class
private Calendar myCalendar = Calendar.getInstance();
private EditText etDate;

//onCreate method of the Activity class
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    etDate = (EditText) findViewById(R.id.et_date);
    etDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new DatePickerDialog(TheClassName.this, datePickerListener, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();        
        }
    });

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

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            String myFormat = "MM/dd/yy";
            SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
            etDate.setText(sdf.format(myCalendar.getTime()));
        }

    };
}

You need to do some changes as below

  1. Initialize the myCalendar to avoid NullPointerException

    myCalendar = new GregorianCalendar();

  2. Declare datePickerListener before using it to avoid Unresolved variable

  3. Change TheClassName.this to your activity class name to get the context; I assumed below it as MainActivity.this

  4. Make EditText & DatePickerDialog.OnDateSetListener as final in order to access them from the EditText OnClickListener callback

So with these changes in place, you can replace your code with below:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText etDate = (EditText) findViewById(R.id.et_date);

    myCalendar = new GregorianCalendar();

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

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            String myFormat = "MM/dd/yy";
            SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
            etDate.setText(sdf.format(myCalendar.getTime()));
        }

    };

    etDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new DatePickerDialog(MainActivity.this, datePickerListener, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM