如何将日期和时间保存到特定按钮?

Posted

技术标签:

【中文标题】如何将日期和时间保存到特定按钮?【英文标题】:How to save date and time to a specific button? 【发布时间】:2020-06-09 11:48:47 【问题描述】:

我目前只有一个按钮来设置闹钟。我可以将闹钟和时间保存到此按钮,但现在我想添加另一个按钮,我可以在其中设置另一个日期和时间。我不知道如何检查单击了哪个按钮,因此将日期保存到此按钮。 这就是我用警报按钮所做的: 我创建了两个类 DatePickerFragment.java

public class DatePickerFragment extends DialogFragment
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) 
    // Use the current date as the default date in the picker
    Calendar c = Calendar.getInstance();
    int jahr = c.get(Calendar.YEAR);
    int monat = c.get(Calendar.MONTH);
    int tag = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(), jahr, monat, tag);


和 TimePickerFragmentDialog.java

public class TimePickerFragment extends DialogFragment 
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) 
    Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, DateFormat.is24HourFormat(getActivity()));


在我的 Activity.java 中有这段代码

private StringBuilder ausgewaelteDatum;
AlarmButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            DialogFragment datePicker = new DatePickerFragment();
            datePicker.show(getSupportFragmentManager(), "date picker");
        
    );

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) 
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    ausgewaelteDatum = new StringBuilder(DateFormat.getDateInstance(DateFormat.MEDIUM).format((c.getTime())));


    DialogFragment timePicker = new TimePickerFragment();
    timePicker.show(getSupportFragmentManager(), "time picker");


@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) 
    ausgewaelteDatum.append(", " + hourOfDay + ":" + minute);
    AlarmButton.setText(ausgewaelteDatum.toString());

现在我为第二个按钮添加了这段代码,但我不知道如何继续。也许我可以在创建新片段时传递单击的按钮,但我不知道这是否可能

DatumButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            DialogFragment datePicker = new DatePickerFragment();
            datePicker.show(getSupportFragmentManager(), "date picker");
        
    );

【问题讨论】:

【参考方案1】:

你可以这样做:

class MainActivity ... 

    private int clicked = 0;

    private static final int NOBODY = 0;
    private static final int ALARM = 1;
    private static final int DATUM = 2;

    private StringBuilder ausgewaelteDatum;
    private Button AlarmButton, DatumButton; //Consider using standard case notation

    @Override
    ... onCreate(...) 
        AlarmButton.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                clicked = ALARM;
                DialogFragment datePicker = new DatePickerFragment();
                datePicker.show(getSupportFragmentManager(), "date picker");
            
        );

        DatumButton.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                clicked = DATUM;
                DialogFragment datePicker = new DatePickerFragment();
                datePicker.show(getSupportFragmentManager(), "date picker");
            
        );
    

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) 
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        ausgewaelteDatum = new StringBuilder(DateFormat.getDateInstance(DateFormat.MEDIUM).format((c.getTime())));

        DialogFragment timePicker = new TimePickerFragment();
        timePicker.show(getSupportFragmentManager(), "time picker");
    

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) 

        ausgewaelteDatum.append(", " + hourOfDay + ":" + minute);

        if (clicked == ALARM)
            AlarmButton.setText(ausgewaelteDatum.toString());
        else if (clicked == DATUM)
            DatumButton.setText(ausgewaelteDatum.toString());

        clicked = NOBODY;
    

如果我理解你的问题。

【讨论】:

谢谢,这正是我所需要的。我刚刚删除了private static final int NOBODY = 0; 行,因为我认为不需要它,而且没有它也可以。

以上是关于如何将日期和时间保存到特定按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何将视频保存到具有特定日期的相机胶卷?

Rails:如何将日期时间字符串解析为特定时区

如何将日期+时间保存和检索到 sqlite3 数据库中?

将日期保存到本地存储

将初始付款延迟到 Paypal 订阅按钮上的特定日期

Laravel如何获取当前时间和日期值