3

共有設定エディターに日付オブジェクトを配置する必要があります。

共有設定に保存するために変換するデータ型は何ですか? 通常 prefEditor.putString("Idetails1", Idetails1);、文字列と要素に対して書き込みます。

それ、どうやったら出来るの?これを日付オブジェクトにも使用できますか?

private EditText pDisplayDate;
private ImageView pPickDate;
private int pYear;
private int pMonth;
private int pDay;
/** This integer will uniquely define the dialog to be used for displaying date picker.*/
static final int DATE_DIALOG_ID = 0;

Date date;

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();
       displayToast();
    }
};

private void updateDisplay() {
    pDisplayDate.setText(
       new StringBuilder()
       // Month is 0 based so add 1
       .append(pMonth + 1).append("/")
       .append(pDay).append("/")
       .append(pYear).append(" ")
    );
}

private void displayToast() {
    Toast.makeText(this, 
        new StringBuilder()
        .append("Date choosen is ")
        .append(pDisplayDate.getText()),
        Toast.LENGTH_SHORT).show();
}
4

2 に答える 2

0

Calendar オブジェクトを使用して、日付が変更されるたびに日付を設定したり、Preference オブジェクトから簡単に取得したりできます。

Calendar c = Calendar.getInstance(); // use system date on first time for initialization.

long millis= pref.getLong("date",c.getTimeInMillis()); // retrieve date from Preference object in long format

c.setTimeInMillis(millis); // now set in calendar and then use it

//on date change set the year, month and day into calendar to and then stored in preference

private DatePickerDialog.OnDateSetListener pDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                pYear = year;
                pMonth = monthOfYear;
                pDay = dayOfMonth;
                c.set(pYear , pMonth , pDay ); // set into the calendar and when need to save in preference do this
                Editor edit = pref.editor();
                edit.put("date",c.getTimeInMillis());
                edit.commit();
                updateDisplay();
                displayToast();
            }
        };
于 2013-04-01T12:26:01.393 に答える