簡體   English   中英

Android DatePicker未出現在DialogFragment上

[英]Android DatePicker is not appearing on DialogFragment

單擊日期字段以使用DatePicker寫入/設置我選擇的日期時,我試圖使日期選擇器出現在對話框片段中。 不幸的是,每次單擊時,DatePicker都不會顯示。

下面是該類的代碼:

public class UpdateGrade extends DialogFragment{

        private EditText dateField;

        static final int DATE_DIALOG_ID = 0;

        protected Dialog onCreateDialog(int id) {
            Calendar c = Calendar.getInstance();
            int cyear = c.get(Calendar.YEAR);
            int cmonth = c.get(Calendar.MONTH);
            int cday = c.get(Calendar.DAY_OF_MONTH);
            switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(getActivity(),  mDateSetListener,  cyear, cmonth, cday);
            }
            return null;
        }
        private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                String date_selected =String.valueOf(monthOfYear+1)+"-"+String.valueOf(dayOfMonth)+"-"+String.valueOf(year);
                dateField.setText(date_selected);
            }
        };

        @SuppressLint("SimpleDateFormat")
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.update_grade_layout, container);
            getDialog().getWindow().requestFeature(STYLE_NO_TITLE);
            dateField = (EditText) view.findViewById(R.id.dateField);

            dateField.setOnTouchListener(new OnTouchListener(){ 

                @SuppressWarnings("deprecation")
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                     if(v == dateField)
                            getActivity().showDialog(DATE_DIALOG_ID);
                        return false;     
                }
            });
            return view;
        }

謝謝!

您必須定義onCreateDialog()方法以返回DatePickerDialog的實例:

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
    }
}

更多信息在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM