首页 > 解决方案 > 将按钮的文本设置为 datepicker 上的选定日期

问题描述

我正在尝试将按钮的文本更改为所选日期以进一步存储在 firebase 数据库中。这是我使用的java代码:

public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }
    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user   
    }
}
public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new MortgageProtectionActivity.DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}

按钮的 ID 是 btn_pf_dob 和 onclick 的 showDatePickerDialog。

标签: javaandroidbuttononclick

解决方案


您可以覆盖该onDateSet方法以在按钮或其他地方放置一个值。要使用对话框片段执行此操作,您可以实现主要活动OnDateSetListener而不是对话框(请参阅此问题问题)。或者,请参阅下面的编辑 2。

编辑1:如果您不需要使用 a DialogFragment,另一种方法是

public void showDatePickerDialog(View v) {
    final Button btn = findViewById(R.id.btn_pf_dob);
    final  DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            // generate string from year, month, and day
            String dateStr = yourMethodHere(year,month,day);
            btn.setText(dateStr);
        }
    };

    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dpd = new DatePickerDialog(this, dateSetListener, year, month, day);
    dpd.show();
}

编辑 2:如果你需要使用 aDialogFragment但不想让你的 Activity 实现onDateSet,你可以使用类似下面的东西。你为什么要这个?如果您有多个从同一个活动启动的日期选择器,您可能希望每个活动都有不同的侦听器。在 Activity 上实现它可以防止这种情况发生,而这种方法可以让您为每个日期选择器创建一个唯一的侦听器。

public static class DatePickerFragment extends DialogFragment {

    private DatePickerDialog.OnDateSetListener listener = null;

    void setListener(DatePickerDialog.OnDateSetListenerlistener) {
        this.listener = listener;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        DatePickerDialog dpd = null;

        if( listener != null ) {
            // Use the current date as the default date in the picker
            Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog
            dpd = new DatePickerDialog(getActivity(), listener, year, month, day);
        }
        return dpd;
    }
}

public void showDatePickerDialog(View v) {
    final Button btn = findViewById(R.id.btn_pf_dob);
    DatePickerFragment newFragment = new DatePickerFragment();
    newFragment.setListener(new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
            String dateStr = "date: " + year + " " + month + " " + day;
            btn.setText(dateStr);
        }
    }
    );
    newFragment.show(getFragmentManager(), "datePicker");
}

要在 Activity 中正确设置它,使用屏幕旋转,您需要在 Activity 中重置监听器,onCreate以便在显示对话框时旋转屏幕,例如

    if (savedInstanceState != null) {
        DatePickerDialogFragment dpf = (DatePickerDialogFragment) getFragmentManager()
                .findFragmentByTag("datePicker");
        if (dpf != null) {
            dpf.setListener(new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int day) {
                    String dateStr = "date: " + year + " " + month + " " + day;
                    btn.setText(dateStr);
                }
            }
            );
        }
    }

推荐阅读