在timePickerFragment上取消按钮的监听器?

时间:2012-08-03 03:28:54

标签: android listener

我会使用TimePickerFragment来设置时间,并且它可以正常工作。

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

不幸的是,到目前为止我还没有找到怎么做。

这是我的代码:

创建对话框:

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

    positionItemTime = position;

}

当用户点击“设置”时执行操作:

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");

}

总结:想要捕捉点击取消按钮事件以执行操作。

感谢。

2 个答案:

答案 0 :(得分:1)

您可以覆盖onCancel方法来执行此操作。

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

   // do your action. you can have your own listener which will can be called.
}

答案 1 :(得分:1)

以下是解决方案,基于覆盖事项:

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然后实现该功能。谢谢大家。