Android:在DatePickerDialog / TimePickerDialog中更改按钮文本 [英] Android: Change button text in DatePickerDialog / TimePickerDialog

查看:394
本文介绍了Android:在DatePickerDialog / TimePickerDialog中更改按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DatePickerDialog。在默认配置下,它可以正常工作。

I have got a DatePickerDialog. In the default configuration it works fine.

DatePickerDialog dialog = new DatePickerDialog(
    view.getContext(),
    new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker picker, int year, int monthOfYear, int dayOfMonth) {
            // do something
        }
    },
    startDate.getYear(),
    startDate.getMonth() - 1,
    startDate.getDay()
);

现在,我想更改按钮的文本。我用以下代码尝试:

Now I want to change the text of the buttons. I try it with the following code:

dialog.setButton(DatePickerDialog.BUTTON_POSITIVE, "New Label", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {
        // do something
    }
});

现在我的问题是从datepicker控件中获取日期。单击按钮不会调用DateSetListener。如何通过DateSetListener获取日期?
获取日期的另一种可能性是

Now my problem is to get the date from the datepicker control. The DateSetListener will not be called by click on the button. How is it possible to get the date via the DateSetListener? Another possibility to get the date is about

public void onClick(DialogInterface dialog, int which) {
    int year = ((DatePickerDialog) dialog).getDatePicker().getYear();
    int month = ((DatePickerDialog) dialog).getDatePicker().getMonth();
    int day = ((DatePickerDialog) dialog).getDatePicker().getDayOfMonth();
}

但这似乎是不必要的复杂方式。

But this seems to be a unnecessary complicated way.

对于TimePickerDialog,此替代方法将不起作用,因为我找不到获得时间选择器控件的方法。因此,我需要使用TimeSetListener,但是我找不到这种可能性(例如日期选择器)。

For the TimePickerDialog this alternative way will not work because I find no way to get the timepicker control. Therefore I need to use the TimeSetListener but I cannot find the possibility (like for the datepicker).

感谢任何响应。

推荐答案

我认为这可以在DatePicker上使用,在TimePicker中对我有用:

I supose that this will work on DatePicker, it works for me in TimePicker:

监听器:

TimePickerDialog.OnTimeSetListener mTimeSetListenerIni =
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(android.widget.TimePicker view,
                            int hourOfDay, int minute) {
                        Log.i("","Initial: "+hourOfDay+":"+minute);
                    }
                };

TimePickerDialog yourFragment = new TimePickerDialog(this, mTimeSetListenerIni, hour, minute, true);
yourFragment.setButton(DialogInterface.BUTTON_POSITIVE, "OK", yourFragment);
yourFragment.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", yourFragment);

这篇关于Android:在DatePickerDialog / TimePickerDialog中更改按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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