在我的应用程序中,我使用this library进行自定义日历视图。单击我通过共享首选项将该日期传递给下一个活动的任何日期。我可以在下一个活动中显示日期。现在我想要的是有两个按钮,一个是“上一个”,另一个是“下一个”。如果我点击任何按钮日期必须根据要求改变。
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块,但无法根据我的要求进行编码。请帮帮我。
答案 0 :(得分:0)
我做到了。这是预期结果的代码。下面是我选择Date并通过SharedPreferences将其传递给NextActivity的代码。
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();
Log.v("You Selected Date is :",newselectedDate);
// SharedPreferences for sharing the Date with Next Activity, Added by Tara
SimpleDateFormat s_day=new SimpleDateFormat("dd",Locale.getDefault()); int sdd= Integer.parseInt(s_day.format(selectedDate));
SimpleDateFormat s_month=new SimpleDateFormat("MM",Locale.getDefault());int smm= Integer.parseInt(s_month.format(selectedDate));
SimpleDateFormat s_year=new SimpleDateFormat("yyyy",Locale.getDefault());int syy= Integer.parseInt(s_year.format(selectedDate));
SharedPreferences spf= getSharedPreferences("myprfs", Context.MODE_PRIVATE);
SharedPreferences.Editor spe = spf.edit();
// spe.putString("sdate", String.valueOf(selectedDate));
spe.putInt("sday", sdd);
spe.putInt("smonth", smm);
spe.putInt("syear", 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(MainActivity.this, ChangeDate.class);
startActivity(i_available_slot);
}
});
以下是使用SharedPreferences从以前的活动获取日期的下一个活动代码
SharedPreferences spf=getSharedPreferences("myprfs", Context.MODE_PRIVATE);
final int id1 = spf.getInt("sday", 0);
final int id2 = spf.getInt("smonth", 0);
final int id3 = spf.getInt("syear", 0);
sdate= id1+"-"+id2+"-"+id3;
date.setText(sdate);
Log.v("Spf Day :", String.valueOf(id1));
Log.v("Spf Month :", String.valueOf(id2));
Log.v("Spf Year :", String.valueOf(id3));
int month_index=id2-1; // Java Maintains Index Number from 0 so reduce the month to 1 to get the selected month
final Calendar c=Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
// c.set(id1, id2, id3);
c.set(Calendar.YEAR,id3);
c.set(Calendar.MONTH,month_index);
c.set(Calendar.DAY_OF_MONTH,id1);
final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = df.format(c.getTime());
Log.v("FormatedDate:",formattedDate);
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
c.add(Calendar.DATE, -1);
formattedDate = df.format(c.getTime());
Log.v("PREVIOUS DATE : ", formattedDate);
date.setText(formattedDate);
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
c.add(Calendar.DATE, 1);
formattedDate = df.format(c.getTime());
Log.v("PREVIOUS DATE : ", formattedDate);
date.setText(formattedDate);
}
});