activity 结束后,您选择的 datepickerdialog 日期如何保留

How the datepickerdialog date you choose remains after the activity ends

对不起 Google 译者

我想要实现的是,即使 activity 结束,用户选择的日期也会保持不变。所以我希望在我重新运行 activity.

时保留设置

我尝试了 SharedPreferences,String 成功但 datapicker 失败 我可以做些什么来保留我的数据选择器用户选择?

这是我使用的代码。

public class Widget extends Activity {
/**
 * Called when the activity is first created.
 */

private TextView ddayText;
private TextView todayText;
private TextView resultText;
private TextView nameText;
private Button dateButton;


private int tYear;          
private int tMonth;
private int tDay;

private int dYear = 1;       
private int dMonth = 1;
private int dDay = 1;


private long d;
private long t;
private long r;
private long w;

private int resultNumber = 0;
private int dayNumber = 0;

private SharedPreferences sf;

static final int DATE_DIALOG_ID = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.widget);

    ddayText = (TextView) findViewById(R.id.dday);
    todayText = (TextView) findViewById(R.id.today);
    resultText = (TextView) findViewById(R.id.result);
    dateButton = (Button) findViewById(R.id.dateButton);
    nameText = (TextView) findViewById(R.id.name);

    dateButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(0);//----------------

        }
    });


    sf = getSharedPreferences("sf", Activity.MODE_PRIVATE);
    String str = sf.getString("name", ""); 
    long dday = sf.getLong("dday", d); 

    nameText.setText(str); 



    Calendar calendar = Calendar.getInstance();             
    tYear = calendar.get(Calendar.YEAR);
    tMonth = calendar.get(Calendar.MONTH);
    tDay = calendar.get(Calendar.DAY_OF_MONTH);

    Calendar dCalendar = Calendar.getInstance();
    dCalendar.set(dYear, dMonth, dDay);

    t = calendar.getTimeInMillis();                 
    d = dCalendar.getTimeInMillis();             
    r = (d - t) / (7 * 24 * 60 * 60 * 1000L);  
    w = (d - t) / (24 * 60 * 60 * 1000L); 


    resultNumber = (int) r * (-1);
    dayNumber = (int) w * (-1);

    updateDisplay();


}//OnCreate end


private void updateDisplay() {

    todayText.setText(String.format("%d년 %d월 %d일", tYear, tMonth + 1, tDay));
    ddayText.setText(String.format("%d년 %d월 %d일", dYear, dMonth + 1, dDay));

    if (resultNumber >= 0) {
        resultText.setText(String.format("임신 %d주 %d일", resultNumber % 45L, dayNumber % 7L + 1));


    } else {

        int absR = Math.abs(resultNumber);
        resultText.setText(String.format("임신 %d주 %d일", absR, dayNumber % 7L));


    }
}

@Override
protected Dialog onCreateDialog(int id) {
    if (id == DATE_DIALOG_ID) {
        return new DatePickerDialog(this, dDateSetListener, tYear, tMonth, tDay);
    }

    return null;
}

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


    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
        final Calendar dCalendar = Calendar.getInstance();
        dCalendar.set(year, monthOfYear, dayOfMonth);

        long d = dCalendar.getTimeInMillis();
        r = (d - t) / (7 * 24 * 60 * 60 * 1000L);
        w = (d - t) / (24 * 60 * 60 * 1000L);

        resultNumber = (int) r * (-1);
        dayNumber = (int) w * (-1);

        saveData(nameText.getText().toString(), d);

        updateDisplay();

    }
    private void saveData(String str, long d) {

        SharedPreferences.Editor editor = sf.edit();

        editor.putString("name", str);
        editor.putLong("dday", d);

        editor.commit();
    }
};

}

我认为如果您想在正确的时间存储数据,您会希望将 onDateSet 内的数据保存到 SharedPreferences。

例如,将首选项移动到一个字段

// Store this as a field for later reference
private SharedPreferences sf;
private final Calendar mCalendar = Calendar.getInstance();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.widget);

    nameText = (TextView) findViewById(R.id.name);

    // Load any data you have 
    sf = getSharedPreferences("sf", Activity.MODE_PRIVATE);
    String str = sf.getString("name", ""); 
    long dday = sf.getLong("dday", -1); 
    // TODO: set mCalendar time using dday
    // if dday == -1, then nothing is saved 

    // load the date picker values 
    tYear = mCalendar.get(Calendar.YEAR);
    tMonth = mCalendar.get(Calendar.MONTH);
    tDay = mCalendar.get(Calendar.DAY_OF_MONTH);

    nameText.setText(str); 
    updateDisplay();

并在侦听器中使用它。添加一个辅助方法来提取写入首选项的逻辑

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


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

        mCalendar.set(year, monthOfYear, dayOfMonth);
        long d = mCalendar.getTimeInMillis();

        saveData(nameText.getText().toString(), d);
        // updateDisplay();
}

private void saveData(String str, long d) {
    SharedPreferences.Editor editor = sf.edit();

    editor.putString("name", str); 
    editor.putLong("dday", d); 

    editor.commit(); 
}