Android: CalendarView 和 Button 无法共享数据
Android: CalendarView and Button not able to share data
我正在创建一个允许用户列出学校作业和截止日期的应用程序。
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_layout);
mCalendarView = (CalendarView) findViewById(R.id.calendarView);
btnConfirmCal = (Button) findViewById(R.id.confirmCalendar);
et_assignName = (EditText) findViewById(R.id.assignName);
et_assignDesc = (EditText) findViewById(R.id.assignDesc);
mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int dayOfMonth) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String selectedDate = sdf.format(new Date(mCalendarView.getDate()));
}
});
btnConfirmCal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
ScheduleList scheduleList = new ScheduleList(et_assignName.getText().toString(),
et_assignDesc.getText().toString(), selectedDate);
Toast.makeText(CalendarActivity.this, scheduleList.toString(), Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(CalendarActivity.this, "Error creating schedule", Toast.LENGTH_SHORT).show();
}
}
});
}
在 OnClickListerner 中,“selectedDate”突出显示为错误。有办法解决这个问题吗?
selectedDate
在另一个范围内定义,因此它对 OnClickListerner 不可见。您可以将其移动到 onCreate 方法范围。
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_layout);
mCalendarView = (CalendarView) findViewById(R.id.calendarView);
btnConfirmCal = (Button) findViewById(R.id.confirmCalendar);
et_assignName = (EditText) findViewById(R.id.assignName);
et_assignDesc = (EditText) findViewById(R.id.assignDesc);
String selectedDate = "";
mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int dayOfMonth) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
selectedDate = sdf.format(new Date(mCalendarView.getDate()));
}
});
btnConfirmCal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
ScheduleList scheduleList = new ScheduleList(et_assignName.getText().toString(),
et_assignDesc.getText().toString(), selectedDate);
Toast.makeText(CalendarActivity.this, scheduleList.toString(), Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(CalendarActivity.this, "Error creating schedule", Toast.LENGTH_SHORT).show();
}
}
});
}
我正在创建一个允许用户列出学校作业和截止日期的应用程序。
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_layout);
mCalendarView = (CalendarView) findViewById(R.id.calendarView);
btnConfirmCal = (Button) findViewById(R.id.confirmCalendar);
et_assignName = (EditText) findViewById(R.id.assignName);
et_assignDesc = (EditText) findViewById(R.id.assignDesc);
mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int dayOfMonth) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String selectedDate = sdf.format(new Date(mCalendarView.getDate()));
}
});
btnConfirmCal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
ScheduleList scheduleList = new ScheduleList(et_assignName.getText().toString(),
et_assignDesc.getText().toString(), selectedDate);
Toast.makeText(CalendarActivity.this, scheduleList.toString(), Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(CalendarActivity.this, "Error creating schedule", Toast.LENGTH_SHORT).show();
}
}
});
}
在 OnClickListerner 中,“selectedDate”突出显示为错误。有办法解决这个问题吗?
selectedDate
在另一个范围内定义,因此它对 OnClickListerner 不可见。您可以将其移动到 onCreate 方法范围。
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_layout);
mCalendarView = (CalendarView) findViewById(R.id.calendarView);
btnConfirmCal = (Button) findViewById(R.id.confirmCalendar);
et_assignName = (EditText) findViewById(R.id.assignName);
et_assignDesc = (EditText) findViewById(R.id.assignDesc);
String selectedDate = "";
mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int dayOfMonth) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
selectedDate = sdf.format(new Date(mCalendarView.getDate()));
}
});
btnConfirmCal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
ScheduleList scheduleList = new ScheduleList(et_assignName.getText().toString(),
et_assignDesc.getText().toString(), selectedDate);
Toast.makeText(CalendarActivity.this, scheduleList.toString(), Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(CalendarActivity.this, "Error creating schedule", Toast.LENGTH_SHORT).show();
}
}
});
}