如何使听者的datepickers在Android的取消按钮? [英] How to make a listener for a datepickers cancel button in Android?

查看:156
本文介绍了如何使听者的datepickers在Android的取消按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Android中一个datepicker,让用户选择的日期。我想要它做的一件事,如果用户选择一个日期,并将其设置(我有工作的罚款),然后当用户按下取消按钮上的日期选择器(打开日期选择器,但随后取消了明确一定的文本字段它)。

I'm using a datepicker in Android to let the user to choose the date. I want it to do one thing if the user picks a date and sets it (I have that working fine) and then to clear a certain text field if the user pushes the cancel button on the datepicker (open up the datepicker but then cancel out of it).

我一直在努力的方式是通过一个

The way I've been trying is by making a

private DatePickerDialog.OnCancelListener mDateCancelListener =
    new DatePickerDialog.OnCancelListener() {
        public void onCancel(DialogInterface dialog) {
            timeClear(); //method that clears text field
        }

    };

然后我

TimePickerDialog timeDialog = new TimePickerDialog(this,
  mTimeSetListener,
  c.get(Calendar.HOUR),
  c.get(Calendar.MINUTE),
  false);
timeDialog.setOnCancelListener(mTimeCancelListener);

附加监听器。

我的问题是,如果用户按下返回按钮听者的工作原理,但如果它们推取消按钮。我试图用解雇侦听器,这样的作品,除了它熄灭我甚至是否设置或取消的日期选择器!

My problem is that the listener works if the user pushes the back button, but not if they push the cancel button. I tried using a dismiss listener, and that works, except for the fact that it goes off even whether I set or cancel the datepicker!

什么我需要做的事情如此熄灭当且仅当我把我的日期选择取消按钮?

What do I need to do so something goes off if and only if I push the cancel button on my datepicker?

推荐答案

唯一的解决办法,直觉是,你需要覆盖的取消键:

The only solution, counter intuitive, is that you need to "override" the cancel button:

    timeDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
            getString(R.string.cancel),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do stuff
                }
            });

这篇关于如何使听者的datepickers在Android的取消按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆