简体   繁体   English

同一活动中有两个DatePicker-Android

[英]Two DatePicker in the same Activity - Android

In my activity I have two TextView (startDatePicker & endDatePicker) and when they are clicked a datePicker dialog should show up. 在我的活动中,我有两个TextView(startDatePicker和endDatePicker),当单击它们时,应该显示一个datePicker对话框。 Until here everything works fine, but I need, after the user choose the date, to set the text of the corresponding clicked textView with the user's choice. 到这里为止一切正常,但是在用户选择日期之后,我需要用用户的选择来设置相应单击的textView的文本。 I found another post like this and I took the cue from it : https://stackoverflow.com/a/24534147/10113273 我发现了另一条这样的帖子,并从中得到了提示: https : //stackoverflow.com/a/24534147/10113273

This is my DatePickerDialogFragment Class: 这是我的DatePickerDialogFragment类:

public class DatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

public static final int START_DATE = 0;
public static final int END_DATE = 1;

private int current = 0;
private int chosenDate;

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

    Bundle bundle = this.getArguments();
    if (bundle != null) {
        chosenDate = bundle.getInt("DATE", 0);
    }
    switch (chosenDate) {
        case START_DATE:
            current = START_DATE;
            return new DatePickerDialog(getActivity(), this, year, month, dayOfMonth);

        case END_DATE:
            current = END_DATE;
            return new DatePickerDialog(getActivity(), this, year, month, dayOfMonth);
    }
    return null;
}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    if (current == START_DATE) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        String dateString = DateFormat.getDateInstance().format(calendar.getTime());

        //Set the startDate textView text with the dateString
    } else {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        String dateString = DateFormat.getDateInstance().format(calendar.getTime());

        //Set the endDate textView text with the dateString
    }
}

} }

Inside the onDateSet how can I access the textView? 在onDateSet内,如何访问textView? I probably think that the answer is that it cannot be done. 我可能认为答案是无法完成的。 If I am right, what could I do to solve this? 如果我是对的,我该怎么做才能解决这个问题?

Here where I set the onClickListener on the textview in the Activity: 在这里我在Activity中的textview上设置onClickListener的位置:

startDatePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putInt("DATE",0);

            DialogFragment dialogFragment = new DatePickerDialogFragment();
            dialogFragment.setArguments(bundle);
            dialogFragment.show(getSupportFragmentManager(),"date picker");
        }
    });

    endDatePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putInt("DATE",1);

            DialogFragment dialogFragment = new DatePickerDialogFragment();
            dialogFragment.setArguments(bundle);
            dialogFragment.show(getSupportFragmentManager(),"date picker");
            //endDatePicker.setText(endDate);
        }
    });

You will probably be better off not having the DatePickerDialogFragment implement the listener (I know that's in the Android examples , but it's an inflexible design as you've noticed). 如果没有DatePickerDialogFragment实现侦听器,您可能会更好(我知道这在Android示例中 ,但是您已经注意到,这是一种不灵活的设计)。 If you add a setListener method to the DatePickerDialogFragment like this: 如果像这样向DatePickerDialogFragment添加setListener方法:

public class DatePickerDialogFragment extends DialogFragment {

    private DatePickerDialog.OnDateSetListener listener = null;

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

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), listener, year, month, dayOfMonth);
    }
}

then it's easy to use with different date pickers, easy to reuse throughout your app, and straightforward to put the result into some other view in the Activity that launches the picker, like this (the example code below would go in your Activity onCreate method) 那么可以很容易地与不同的日期选择器一起使用,易于在整个应用中重用,并且可以直接将结果放入启动选择器的活动中的其他视图中,例如(下面的示例代码将放在您的Activity onCreate方法中)

final DatePickerDialog.OnDateSetListener startListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        String dateString = DateFormat.getDateInstance().format(calendar.getTime());

        // set the start date TextView
        startDatePicker.setText(dateString);
    }
};

final DatePickerDialog.OnDateSetListener endListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        String dateString = DateFormat.getDateInstance().format(calendar.getTime());

        // set the enddate TextView
        endDatePicker.setText(dateString);
    }
};

// NOTE: Use getSupportFragmentManager or getFragmentManager below
//       depending on which type of DialogFragment you extended

// Reset the listener if the screen was rotated
if (savedInstanceState != null) {
    DatePickerDialogFragment dpf;

    dpf = (DatePickerDialogFragment) getSupportFragmentManager().findFragmentByTag("myStartDatePicker");
    if (dpf != null) {
        dpf.setListener(startListener);
    }

    dpf = (DatePickerDialogFragment) getSupportFragmentManager().findFragmentByTag("myEndDatePicker");
    if (dpf != null) {
        dpf.setListener(endListener);
    }
}

startDatePicker.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DatePickerDialogFragment dpf = new DatePickerDialogFragment();

        // set bundle args on dpf if still needed

        dpf.setListener(startListener);
        dpf.show(getSupportFragmentManager(), "myStartDatePicker");
    }
});

endDatePicker.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DatePickerDialogFragment dpf = new DatePickerDialogFragment();

        // set bundle args on dpf if still needed

        dpf.setListener(endListener);
        dpf.show(getSupportFragmentManager(), "myEndDatePicker");
    }
});

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

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