简体   繁体   English

Android DatePickerDialog等待关闭

[英]Android DatePickerDialog wait for dismiss

Today imma trying to develop a simple app, which will ask the user to insert 2 dates by using the DatePickerDialog through a button, and then execute a query into a Database: 今天,imma尝试开发一个简单的应用程序,该应用程序将要求用户通过一个按钮使用DatePickerDialog插入两个日期,然后对数据库执行查询:

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

        }

    });

My only question is: How can i wait for the DatePickerDialog do dismiss or wait for the user to enter the input and then execute the query ? 我唯一的问题是:如何等待DatePickerDialog关闭或等待用户输入输入然后执行查询?

Per the docs : 根据文档

We recommend that you use DialogFragment to host each time or date picker. 我们建议您使用DialogFragment来托管每个时间或日期选择器。

The docs include a sample class that extends DialogFragment (located here ): 该文档包括延伸的样品类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
    }
}

And you would show it like this (again, code from the docs ): 您将像这样显示它(再次,来自docs的代码):

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

And, as an added bonus, this dialog would indeed be modal (what you were going for when you said "wait for dismiss"). 而且,作为一个额外的好处,该对话框确实是模态对话框(当您说“等待解雇”时您要做什么)。

It's a simple dialog I don't see why you don't use the default listeners: 这是一个简单的对话框,我看不到为什么不使用默认侦听器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM