onDateChanged方法似乎不起作用

时间:2014-10-27 14:04:37

标签: android android-dialogfragment datapicker

这是我的第一篇文章,我正在尝试创建一个非常简单的任务应用程序,但是我遇到了问题(似乎是)onDateChanged方法。即使对话框正常弹出,它似乎也没有实现更改。日志中没有错误,我仔细检查了具有类似功能的各种解决方案,但我找不到相关的东西。 (minSDK 16,目标21)

非常感谢,如果有明显的事情,我很抱歉。



 
public class DatePickerFragment extends DialogFragment {

    public static final String EXTRA_DATE = "thanoschatz.com.annastodolist.date";

    private Date mDate;

    public static DatePickerFragment newInstance(Date mDate) {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_DATE, mDate);

        DatePickerFragment fragment = new DatePickerFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDate = (Date) getArguments().getSerializable(EXTRA_DATE);


        Calendar calendar = Calendar.getInstance();
        calendar.setTime(mDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);

        DatePicker datePicker = (DatePicker) v.findViewById(R.id.dialog_date_datepicker);
        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                mDate = new GregorianCalendar(year, monthOfYear, dayOfMonth).getTime();


                getArguments().putSerializable(EXTRA_DATE, mDate);
            }
        });

        return new AlertDialog.Builder(getActivity())
                .setView(v) //
                .setTitle(R.string.date_picker_title) //
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        sendResult(Activity.RESULT_OK);
                    }
                })
                .create();
    }

    private void sendResult(int resultCode) {
        if (getTargetFragment() == null) return;

        Intent intent = new Intent();
        intent.putExtra(EXTRA_DATE, mDate);

        getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);
    }
}

 

public class ToDoFragment extends Fragment {
    public static final String EXTRA_TODO_ID = "thanoschatz.com.annastodolist.todo_id";
    private static final String DIALOG_DATE = "date";
    private static final int REQUEST_DATE = 0;

    private ToDo mToDo;
    private EditText mTitleField;
    private Button mDateButton;
    private CheckBox mDoneCheckBox;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        UUID toDoId = (UUID)getArguments().getSerializable(EXTRA_TODO_ID);
        mToDo= ToDoLab.get(getActivity()).getToDo(toDoId);
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_todo, container, false);
        wireTitleField(view);
        wireDateButton(view);
        wireSolvedCheckBox(view);
        return  view;
    }




    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == REQUEST_DATE) {
            Date mDate = (Date)intent.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
            mToDo.setDate(mDate);
           
        }
    }

    public ToDoFragment(){

    }

    
    public static ToDoFragment newInstance(UUID mId) {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_TODO_ID, mId);

        ToDoFragment fragment = new ToDoFragment();
        fragment.setArguments(args);

        return fragment;
    }

  

    private void wireDateButton(View view) {
        SimpleDateFormat dateFormatter = getSimpleDateFormat();
        mDateButton = (Button)view.findViewById(R.id.toDo_date);
        updateDate(dateFormatter);

        mDateButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                FragmentManager fragmentManager = getFragmentManager();
                DatePickerFragment datePicker = DatePickerFragment.newInstance(mToDo.getDate());
                datePicker.setTargetFragment(ToDoFragment.this, REQUEST_DATE);
                datePicker.show(fragmentManager, DIALOG_DATE);
            }
        });
    }
}




4 个答案:

答案 0 :(得分:11)

您在Android 5.0下运行您的应用,对吗?我发现5.0以下的DatePicker在默认的CalendarView模式下不会调用OnDateChanged()方法,但如果是Spinners模式则会这样做。如果DatePicker显示为微调器,则代码应该有效。

我发现强制5.0 DatePicker进入Spinners模式的唯一方法是在定义DatePicker的xml代码中包含以下内容:

android:datePickerMode="spinner"

Lollipop似乎忽略了任何android:CalendarViewShown =" false"指令以及Java代码中的所有DatePicker.setCalendarViewShown(false)调用。 datePickerMode xml指令仅在API 21中受支持,但在较低API级别运行的任何设备都将忽略它,因此您的代码仍将在较旧版本的Android上运行。保持CalendarViewShown =" false"但是,如果你想让DatePicker成为5.0之前的设备上的微调器,你的xml代码中的指令。

答案 1 :(得分:1)

改为使用DatePickerDialog

return new DatePickerDialog(getActivity(), this, year, month, day);

您的班级声明应如下所示:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

您需要实施OnDateSet方法:

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    mDate = new GregorianCalendar(year, monthOfYear, dayOfMonth).getTime();
    getArguments().putSerializable(EXTRA_DATE, mDate);

    sendResult(Activity.RESULT_OK);
}

您可以将主题设置为Holo.Light,它似乎支持DatePicker OnDateChanged事件。在您的清单文件中:

<application android:label="@string/app_name"
             android:icon="@drawable/ic_launcher"
             android:theme="@android:style/Theme.Holo.Light">

您可以降低最低API级别,默认情况下会设置Holo主题:

<uses-sdk android:minSdkVersion="15" />

答案 2 :(得分:1)

还有另一种方法可以让它在5.0下工作并使DatePicker保持日历模式。您可以执行的操作设置为侦听对话框上的单击更改,并更新日期参考。如果您的关闭对话框处理程序发现任何机会注意到单击的最后一个按钮是“取消”,那么只需忽略更改...

private String dateString; //last date saved.
    private String clickedDate; //last date clicked on DatePicker
    private DatePicker datePicker;

@Override
    protected void onDialogClosed(boolean shouldSave) {
        if (shouldSave && this.clickedDate != null) {
            updateDate(this.clickedDate);
            this.clickedDate = null;
        }
    }

@Override
    public void onDateChanged(DatePicker view, int year, int month, int day)       {
        Calendar selected = new GregorianCalendar(year, month, day);
        this.clickedDate = formatter().format(selected.getTime());
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        super.onClick(dialog, which);
        datePicker.clearFocus();
        onDateChanged(datePicker, datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth());
        onDialogClosed(which == DialogInterface.BUTTON_NEGATIVE);
    }

private void updateDate(String s) {
dateString = s;
}

这就是https://github.com/bostonandroid/DatePreference/blob/master/DatePreference/src/org/bostonandroid/datepreference/DatePreference.java

的想法

答案 3 :(得分:0)

这是我的答案,可以简单地解决这个问题:

//Serial Inputs - To be used for the input drivers, agents and monitors
dai_if   #(.P_WD_DATA (P_WD_DATA)) dai_ser_ivif();

uvm_config_db#(virtual dai_if#(P_WD_DATA))::set(uvm_root::get(), "uvm_test_top.dai_env.dai_ag_in[0]*",  "dai_vif", dai_ser_ivif);