DatePickerDialog 和 TimePickerDialog 不能同时工作

DatePickerDialog and TimePickerDialog Are Not Working at the Same Time

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

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

如有任何帮助,我们将不胜感激

这是我的 MainActivity class(我从中删除了我的 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. 定义全局日历变量。

    Calendar selecteddate;
    
  2. 显示日期选择器,先定义日历,再定义日期选择器。

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

让我知道这是否适合你。