我有从DialogFragment扩展的DatepickerFragment。在我的主要片段中有两个edittext。第一个用于开始日期,第二个用于完成日期。这些是针对某些用户操作的。用户单击第一个edittext选择日期,而另一个单击的方式相同,但是我对如何处理DatepickerFragment上的edittext感到困惑。什么是最佳方法?

    etOverFlowingStaringDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            FragmentManager fragmentManager = getActivity().getFragmentManager();
            DatePickerFragment datePickerFragment = new DatePickerFragment();
            datePickerFragment.show(fragmentManager, "datepicker");
        }
    });


DatepickerFragment.java

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

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        populateSetDate(year, monthOfYear+1, dayOfMonth);
    }

    private void populateSetDate(int year, int month, int dayOfMonth) {

       String datetime = dayOfMonth + "/" + month + "/" + year ;

        //EditText edtext = (EditText)getView().findViewById(R.id.overflow_starting_date);
        // The edittext is changable it is not a good way to define in here

    }
}

最佳答案

我通常在单独的类中创建自定义DatePickerFragment,然后使用Listener模型来实现您要实现的目标。实质上:

在您的DatePickerFragment中,定义一个接口-

public interface OnDateChosenListener {
    void onDateChosen(int year, int month, int day);
}


然后,在DatePickerFragment类中为该接口定义一个私有变量-

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    private OnDateChosenListener mOnDateChosenListener = null;

    ...


添加一个函数来设置OnDateChosenListener-

public void setOnDateChosenListener(OnDateChosenListener listener) {
    mOnDateChosenListener = listener;
}


DatePickerFragment类的最后一件事,将事件和值传递给侦听器(如果存在)-

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    if (mOnDateChosenListener != null) {
        mOnDateChosenListener.onDateChosen(year, monthOfYear, dayOfMonth);
    }
}


最后,最重要。请记住,无论您在何处使用新的DatePickerFragment,都应添加一个侦听器,在这种情况下,就是一个Activity-

FragmentManager fragmentManager = getActivity().getFragmentManager();
DatePickerFragment datePickerFragment = new DatePickerFragment();
datePickerFragment.addOnDateChosenListener(new OnDateChosenListener() {
    public void onDateChosen(int year, int month, int day) {
        // use values to change EditText value
    }
}
datePickerFragment.show(fragmentManager, "datepicker");


侦听器模型是一种很好的做法,已在Android框架中广泛使用。

关于java - 如何使用Datepicker对话框拖动不同的EditText,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37125397/

10-10 08:48