我正在尝试创建一个小型应用程序,目标是单击日期时上传图像。图像文件以mm-dd-yyyy
格式存储。假设我单击2018年5月1日,必须上传相应的图像。
我试图通过调用Intent in from the calendar class
启动一个新活动,但是该活动没有启动。
这是我的日历课程代码。如何将特定日期设置为按钮?我尝试查看其他示例,但并没有太大帮助。
Calendar Class
public class CalendarApp extends AppCompatActivity {
CalendarView calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar_app);
initializeCalendar();
}
public void initializeCalendar(){
calendar=(CalendarView) findViewById(R.id.calendar);
// sets whether to show the week number.
calendar.setShowWeekNumber(false);
//Set first day of Week as Monday
calendar.setFirstDayOfWeek(2);
//Set Background color for the Selected Week
calendar.setSelectedWeekBackgroundColor(getResources().getColor(R.color.green));
//Set colors for the date of unfocussed month
calendar.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent));
//Set colors for separators line between weeks
calendar.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent));
//Set colors for the vertical bars between the selected start date and the selected end date
calendar.setSelectedDateVerticalBar(R.color.darkgreen);
//Set the listener to be notified upon selected date change
calendar.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
Toast.makeText(getApplicationContext(),
day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
startActivity(new Intent(CalendarApp.this, Image_View.class));
}
}
);
}
}
Image_View.java
public class Image_View extends Activity {
Button button;
ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_upload);
addListenerOnButton();
}
public void addListenerOnButton() {
image = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.Click);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
image.setImageResource(R.drawable.may_1st_2018);
}
});
}
}
最佳答案
这样做:
calendar.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
Toast.makeText(getApplicationContext(),
day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
if(day==30 && month==4 && year==2018)
{
startActivity(new Intent(CalendarApp.this, Image_View.class).putExtra("date",day+"_"+month+"_"+year));
}
}
}
);
同样不要忘记在清单中添加Image_View活动。