看看谁可以提供帮助。我希望在关闭应用程序时,我保存datepicker的日期,以便在重新打开时。你能救我吗?
public class MainActivity extends AppCompatActivity {
private TextView et1, et2, et3;
private int mYear, mMonth, mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(TextView)findViewById(R.id.et1);
et2=(TextView)findViewById(R.id.et2);
et1.setText("");
et2.setText("");
}
public void inttroduce_fecha(View v) {
final Calendar dateIni = Calendar.getInstance();
mYear = dateIni.get(Calendar.YEAR);
mMonth = dateIni.get(Calendar.MONTH);
mDay = dateIni.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth){
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth));
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = format.format(calendar.getTime());
et1.setText(dateString);
Calendar calendar1 = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth+28));
SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yyyy");
String dateString1 = format.format(calendar.getTime());
et2.setText(dateString1);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
}
答案 0 :(得分:0)
没找到,为什么?
公共类MainActivity扩展AppCompatActivity { 私有TextView et1,et2;
private int mYear, mMonth, mDay;
private String MY_PREFS_NAME = "mypref";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(TextView)findViewById(R.id.et1);
et2=(TextView)findViewById(R.id.et2);
et1.setText("");
et2.setText("");
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt("idName", 12);
editor.apply();
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
int et1 = prefs.getInt("idName", 0); //0 is the default value.
}
}
public void inttroduce_fecha(View v) {
final Calendar dateIni = Calendar.getInstance();
mYear = dateIni.get(Calendar.YEAR);
mMonth = dateIni.get(Calendar.MONTH);
mDay = dateIni.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth){
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth));
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = format.format(calendar.getTime());
et1.setText(dateString);
Calendar calendar1 = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth+28));
SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yyyy");
String dateString1 = format.format(calendar.getTime());
et2.setText(dateString1);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
}