在我的应用程序中,我使用this library进行自定义日历视图。点击任何日期我将通过共享偏好将该日期传递给下一个活动。我可以在下一个活动中显示日期。现在我想要的是有两个按钮,一个是“Previous”,另一个是“Next”。如果我点击任何按钮的日期必须根据需要改变。获取上一个/下一个日期使用SharedPreferences从上一活动获取日期后按下按钮上的日期android
的onclick日期代码是
calendarView.setOnDateClickListener(new CalendarView.OnDateClickListener() {
@Override
public void onDateClick(@NonNull Date selectedDate) {
SimpleDateFormat df_new = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
String newselectedDate=df_new.format(selectedDate);
Toast.makeText(getApplicationContext(), "You selected" + newselectedDate + " : Date", Toast.LENGTH_SHORT).show();
// SharedPreferences for sharing the Date with Next Activity, Added by Tara
SimpleDateFormat s_day=new SimpleDateFormat("dd",Locale.getDefault()); String sdd=s_day.format(selectedDate);
SimpleDateFormat s_month=new SimpleDateFormat("MM",Locale.getDefault());String smm=s_month.format(selectedDate);
SimpleDateFormat s_year=new SimpleDateFormat("yyyy",Locale.getDefault());String syy=s_year.format(selectedDate);
SharedPreferences spf= getSharedPreferences("myprfs", Context.MODE_PRIVATE);
SharedPreferences.Editor spe = spf.edit();
// spe.putString("sdate", String.valueOf(selectedDate));
spe.putString("sday", String.valueOf(sdd));
spe.putString("smonth", String.valueOf(smm));
spe.putString("syear", String.valueOf(syy));
Log.d("Day is", String.valueOf(sdd));
Log.d("Month is", String.valueOf(smm));
Log.d("Year is", String.valueOf(syy));
spe.commit();
Intent i_available_slot=new Intent(BookAnAppoinment.this, ChangeDate.class);
startActivity(i_available_slot);
}
});
和未来我在哪里,通过共享偏好获取最新的活动是
SharedPreferences spf=getSharedPreferences("myprfs", Context.MODE_PRIVATE);
String id1 = spf.getString("sday", "no value");
String id2 = spf.getString("smonth", "no value");
String id3 = spf.getString("syear", "no value");
String sdate=id1+"-"+id2+"-"+id3;
// d.setText(sdate);
final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
final String formattedDate = df.format(sdate);
d.setText(formattedDate);
try {
Date date=df.parse(sdate);
Calendar cal = df.getCalendar();
} catch (ParseException e) {
e.printStackTrace();
}
n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
p.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
我写try/catch块,但无法代码按我需求。请帮助我。
2016-05-16
Tara