DatePickerDialog和TimePickerDialog不能同时工作

时间:2016-03-29 06:53:49

标签: android datepickerdialog timepickerdialog

我是Android新手。我一直在开发我的第一个应用程序,这是一个提醒。在提醒中,我创建了两个EditText,一个用于从TimePickerDialog中选择时间,另一个用于从DatePickerDialog中选择日期。当我同时包含(TimePickerDialog和DatePickerDialog)时,为它们设置的时间被视为单独的时间,并且每个时间都会在它应该的时候消失。基本上,两个警报将会关闭,而不是一个。

我应该如何包含TimePickerDialog和DatePickerDialog,以便在设置时将它们视为一个警报?

非常感谢任何帮助

这是我的MainActivity类(我从中删除了我的TimePickerDialog部分):

MainActivity.java

public class MainActivity extends Activity {

EditText editText;
TextView textView;
Spinner spinner;
ArrayAdapter<CharSequence> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);
    spinner = (Spinner) findViewById(R.id.spinner);
    adapter = ArrayAdapter.createFromResource(MainActivity.this,
            R.array.Alarms, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, parent.getItemAtPosition(position) + " is selected", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    editText = (EditText) findViewById(R.id.editText);
    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            editText.setText("");
            openTimePickerDialog(false);
        }
    });

}

private void openTimePickerDialog(boolean is24r) {
    Calendar calendar = Calendar.getInstance();
    TimePickerDialog timePickerDialog =
            new TimePickerDialog(
                    MainActivity.this,
                    onTimeSetListener,
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    is24r);

    timePickerDialog.setTitle("Set time");
    timePickerDialog.show();
}


TimePickerDialog.OnTimeSetListener onTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                textView.setText(hourOfDay + ":" + minute);

                setAlarm(calendar);
                setAlarmTwo(calendar);
            }
        };


private void setAlarm(Calendar targetCal) {

    if (spinner.getSelectedItem().toString().equals("none")) {
        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 2, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

        textView.setText(
                "\n\n***\n"
                        + "One shot alarm " + "/n" + targetCal.getTime());
    }

}

private void setAlarmTwo(Calendar targetCalendar) {
    if (spinner.getSelectedItem().toString().equals("two mins")) {

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 5, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                targetCalendar.getTimeInMillis(),
                TimeUnit.MINUTES.toMillis(2),
                pendingIntent);

        textView.setText(
                "\n\n***\n"
                        + "The alarm will go off every 2 minutes " + "\n" + targetCalendar.getTime() + "\n"
                        + "***\n");
    }

}
}

1 个答案:

答案 0 :(得分:1)

将此应用于您的计划 -

  1. 定义全局日历变量。

    Calendar selecteddate;
    
  2. 用于显示datepicker,首先定义日历,然后定义日期选择器。

    final Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog startdatepicker = new DatePickerDialog(MainActivity.this,
                    new DatePickerDialog.OnDateSetListener() {
    
                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {
    
                            final Calendar c = Calendar.getInstance();
                            c.set(year, monthOfYear, dayOfMonth);
                            selecteddate = c;
    
    
                        }
                    }, mYear, mMonth, mDay);
            startdatepicker.show();
    
  3. 然后是时间戳。

    TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
                    new TimePickerDialog.OnTimeSetListener() {
    
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            selecteddate.set(selectedinitialdate.get(Calendar.YEAR), selecteddate.get(Calendar.MONTH), selecteddate.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
                        }
                    }
                    , 10, 10, false
    
            );
            timePickerDialog.show();
    
  4. 然后您有一个日历变量,可以将其分配给警报功能。

    将选定日期传递给该功能,您将在用户选择的时间和日期触发一个警报。

    private void setAlarm(Calendar targetCal) {
    
    
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 2, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
    
    }
    
  5. 请告诉我这是否适合您。