0

我的应用程序中有一个 DatePicker。我需要获取所选日期的值???

例如:

 dateDisplay=(EditText)findViewById(R.id.date);

正如我们在普通的edittext中所做的那样:

final EditText Name = (EditText) this.findViewById(R.id.nametext); 
and we can cast the data of it by using `Name.getText().toString()`

同样,我们如何将日期选择器的值获取到字符串?

4

2 回答 2

2

检查这个:

Android:在片段中获取 DatePickerDialog


这很简单:

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

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

DatePickerDialog d = new DatePickerDialog(getActivity(),
        R.style.MyThemee, mDateSetListener, mYear, mMonth, mDay);
d.show();

更新显示方法:

private void updateDisplay() {

    GregorianCalendar c = new GregorianCalendar(mYear, mMonth, mDay);
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");

    editDate.setText(sdf.format(c.getTime()));

    sdf = new SimpleDateFormat("yyyy-MM-dd");

    transDateString=sdf.format(c.getTime());
}// updateDisplay
于 2013-02-18T12:21:03.007 回答
0

这将像魅力一样起作用。

 final Calendar cal = Calendar.getInstance();
       pYear = cal.get(Calendar.YEAR);
       pMonth = cal.get(Calendar.MONTH);
       pDay = cal.get(Calendar.DAY_OF_MONTH);
//----------To Open datePicker----------------
private DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener()
     {
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
            {
                pYear = year;
                pMonth = monthOfYear;
                pDay = dayOfMonth;
                updateDisplay();
            }
     };

//----------To display date in EditText----------------
private void updateDisplay()
     {
           final Calendar cal = Calendar.getInstance();
           currentYear = cal.get(Calendar.YEAR);

           displayBirthdayDate.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(pMonth + 1).append("/")
                        .append(pDay).append("/")
                        .append(pYear));
     }
于 2013-02-18T12:37:07.127 回答