1

我会使用 TimePickerFragment 来设置时间,它可以完美地工作。

我只想在用户单击“取消”按钮时执行一个操作。

不幸的是,到目前为止我还没有找到如何做到这一点。

这是我的代码:

创建对话框:

public void changeTime(int position) {
    df = new TimePickerFragment();
    df.setActivity(activity);
    df.show(getSupportFragmentManager(), "timePicker");

    positionItemTime = position;

}

当用户点击“set”时执行一个动作:

public void setTime(int hourOfDay, int minute) {

    visibleCursorNotes.moveToPosition(positionItemTime);

    int idSelectedRow = visibleCursorNotes.getInt(visibleCursorNotes.getColumnIndex("_id"));

    String content = visibleCursorNotes.getString(visibleCursorNotes.getColumnIndex("content_note"));

    String minuteString = "";
    String hourOfDayString = "";

    minuteString =  "" + minute;
    hourOfDayString = "" + hourOfDay;

    if (minute < 10){
        minuteString = "0" + minuteString;
    }

    if (hourOfDay < 10){
        hourOfDayString = "0" + hourOfDayString;
    }

    String time = "" + hourOfDayString + ":" + minuteString ;

    if (content.equals("")){
        content = "add note ...";
    }

    nds.updateNote(idSelectedRow, content, time);

    visibleCursorNotes = nds.getCursorUpdatedDatabaseForADate(visibleDate);
    updateData();

    visibleListView.setSelection(positionItemTime);

    activity.managerOfSound("write note");

}

总结:想要捕捉点击取消按钮事件来执行一个动作。

谢谢。

4

2 回答 2

2

这是解决方案,基于覆盖的东西:

public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener,
TimePickerDialog.OnDismissListener{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }


    public void onDismiss(DialogInterface dialog){

        System.out.println("oui c'est très bon !");

    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
        System.out.println("voila");
    }
}

该技巧的主要部分是实现 TimePickerDialog.OnDismissListener 然后实现该功能。感谢大家。

于 2012-08-03T09:05:18.997 回答
1

您可以重写 onCancel 方法来执行此操作。

@Override
public void onCancel(DialogInterface dialog)
{
   super.onCancel(); 

   // do your action. you can have your own listener which will can be called.
}
于 2012-08-03T04:37:56.567 回答