在 Android 上触摸 CalendarView 中的选定日期时无法生成事件
我在使用 CalendarView 时遇到问题。
我希望 Activity1 中的用户转到 Activity2 中的日历,然后触摸日期,然后转到 Activity3。
目前,如果用户想要触摸已选择的日期(默认情况下使用今天的日期)继续,他不能,因为日历的唯一事件是 OnDateChangeListener
并且我无法让它工作与 OnClick
或 OnTouch。
有人知道让它工作的方法吗?
这是我尝试过的:
public class CalendarioActivity extends Activity {
private CalendarView calendar;
@Override
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
calendar = new CalendarView(this);
calendar.setOnDateChangeListener(_DateSetListener);
calendar.setOnClickListener(_OnClickListener);
setContentView(calendar);
}
private OnClickListener _OnClickListener = new OnClickListener() {
public void onClick(View v) {
Date date = new Date(calendar.getDate());
Intent i=new Intent(CalendarioActivity.this,SelecionarHoraActivity.class);
i.putExtra("data",
new int[] {date.getDay(),date.getMonth(),date.getYear()});
startActivity(i);}};
private OnDateChangeListener _DateSetListener = new OnDateChangeListener() {
public void onSelectedDayChange(CalendarView view,
int year,
int month,
int dayOfMonth) {
Intent i = new Intent(CalendarioActivity.this,
SelecionarHoraActivity.class);
i.putExtra("data", new int[] {dayOfMonth, month, year});
startActivity(i);}};
}
I encountered an issue using CalendarView.
I want the user that is in activity1 go to the calendar in activity2 and then touch a date then go to activity3.
Currently, if the user wants to touch an already selected date (it uses today's date by default) to move on, he can't because the only event for calendar is OnDateChangeListener
and I cannot get it to work with OnClick
or OnTouch.
Anybody know a way to to make it work?
This is what I tried:
public class CalendarioActivity extends Activity {
private CalendarView calendar;
@Override
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
calendar = new CalendarView(this);
calendar.setOnDateChangeListener(_DateSetListener);
calendar.setOnClickListener(_OnClickListener);
setContentView(calendar);
}
private OnClickListener _OnClickListener = new OnClickListener() {
public void onClick(View v) {
Date date = new Date(calendar.getDate());
Intent i=new Intent(CalendarioActivity.this,SelecionarHoraActivity.class);
i.putExtra("data",
new int[] {date.getDay(),date.getMonth(),date.getYear()});
startActivity(i);}};
private OnDateChangeListener _DateSetListener = new OnDateChangeListener() {
public void onSelectedDayChange(CalendarView view,
int year,
int month,
int dayOfMonth) {
Intent i = new Intent(CalendarioActivity.this,
SelecionarHoraActivity.class);
i.putExtra("data", new int[] {dayOfMonth, month, year});
startActivity(i);}};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试通过
calendar.setClickable(true)
将CalendarView
启用为可点击。这将启用点击事件。我不知道它是否会针对各个日期执行此操作,但如果我理解正确的话,它会启用您的
onClickListener
。Did you try enabling the
CalendarView
as clickable viacalendar.setClickable(true)
. That would enable the click events.I don't know if it would do it for the individual dates though, but it would enable your
onClickListener
if I understand it correctly.