2 loveaass loveaass 于 2016.09.25 18:29 提问

Timerdialog的onTimeSet()方法为什么在安卓5.0以上不被调用? 100C

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker

    // TimePicker mTimePicker = new TimePicker(getActivity());
    // mTimePicker.setIs24HourView(true);
    // int h = mTimePicker.getCurrentHour();

    final Calendar c = Calendar.getInstance();
    c.set(Calendar.AM_PM, 0);
    c.set(Calendar.MINUTE, 1);
    int hour = c.get(Calendar.AM_PM);
    int minute = c.get(Calendar.MINUTE);
    // DateFormat.is24HourFormat(getActivity())
    TimePickerDialog timepickerdialog = new TimePickerDialog(getActivity(),3,
            this, hour, minute,false);

    timepickerdialog.setTitle("预约时间");

    timepickerdialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // FIRE ZE MISSILES!
                    affirmFlag = true;
                }
            });

    timepickerdialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // FIRE ZE MISSILES!
                }
            });
    // Create a new instance of TimePickerDialog and return it
    return timepickerdialog;
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
    Log.e("TIME", "1234");
    mHour = hourOfDay;
    mMinute = minute;
    total = mHour * 60 + mMinute;
    Message msg = mhandler.obtainMessage();
    msg.what = 83;
    msg.arg1 = total;
    if (affirmFlag == true) {
        affirmFlag = false;
        mhandler.sendMessage(msg);
    }
}



以上是部分程序代码,总之在安卓5.0以下onTimeSet方法是被回调的,但在5.0以上就不行,试了网上很多方法都不行,望大神相助

3个回答

devmiao
devmiao   Ds   Rxr 2016.09.25 23:57
loveaass
loveaass 没用
大约一年之前 回复
u011606457
u011606457   2016.09.26 08:34
 new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
loveaass
loveaass 回复JFlex: 我问的是timePickerDialog
大约一年之前 回复
englishbage
englishbage 回复loveaass: mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener(){public void onTimeChanged(TimePicker view, int hourOfDay, int minute){}})
大约一年之前 回复
loveaass
loveaass 這方法也試過,還是沒用
大约一年之前 回复
lzw2497727771
lzw2497727771   2016.09.27 03:53

在使用TimePickerDialog的时候,有个小bug要处理一下,
代码中使用了匿名子类的写法,目的是为了重写父类TimePickerDialog中的onStop方法。因为在android4,.1-4.3(经过测试的版本)中存在着一个bug。我们知道点击确定的时候,会分别回调函数onTimeSet(),这样点击一次按钮竟然回调两次函数,显然是不合理的。但是代码的写法上又没有什么错误,为什么会出现这个问题呢。通过查看源码得知,点击确认按钮完,dialog的onStop()会被执行,
onStop()的源码:
@Override

protected void onStop() {

tryNotifyTimeSet();

super.onStop();

}

再查看按钮被点击时方法:
public void onClick(DialogInterface dialog, int which) {

tryNotifyTimeSet();

}

同样调用了tryNotifyTimeSet(),那这个方法又是什么?
private void tryNotifyTimeSet() {

if (mCallback != null) {

mTimePicker.clearFocus();

mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),

mTimePicker.getCurrentMinute());

}

}

我的天呀,可以看出来,这就是onTimeSet()回调函数被执行了两次的罪魁祸首。知道问题了就好解决了,我们在创建实例的时候加上方法体,重写里面的onStop()方法就行了。这样相当于继承了TimePickerDialog类的子类的实例,但是只使用一次,没必要真的创建一个子类,所以使用匿名的方法,屏蔽了一下这个BUG

Csdn user default icon
上传中...
上传图片
插入图片