0

今天我尝试开发一个简单的应用程序,它将要求用户通过一个按钮使用 DatePickerDialog 插入 2 个日期,然后对数据库执行查询:

        Button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {


          Da = new DatePickerDialog(MainActivity.this, DADateSetListener, 2014, 8, 24);
          Da.show();
          Da.setTitle("Date FROM");

          A = new DatePickerDialog(MainActivity.this, ADateSetListener, 2014, 10, 24);

          A.show();
          A.setTitle("Date TO");


          //Erase the List View
          List.setAdapter(null);

          prepareProgressBar();
          query();

        }

    });

我唯一的问题是:我如何等待 DatePickerDialog 关闭或等待用户输入输入然后执行查询?

4

2 回答 2

0

根据文档

我们建议您使用DialogFragment托管每个时间或日期选择器。

文档包括一个扩展的示例类DialogFragment(位于此处):

public static class TimePickerFragment extends DialogFragment
                            implements TimePickerDialog.OnTimeSetListener {

    @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 onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
    }
}

你会像这样展示它(再次,来自文档的代码):

public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");
}

而且,作为一个额外的好处,这个对话框确实是模态的(当你说“等待解雇”时你想要什么)。

于 2014-09-24T19:42:08.957 回答
0

这是一个简单的对话框,我不明白您为什么不使用默认侦听器:

DatePickerDialog dpd;
dpd.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        // Handle dismiss
    }
});
dpd.setOnKeyListener(new DialogInterface.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        // Handle clicks
        return false;
    }
});
于 2014-09-24T19:45:43.827 回答