Android how to show DatePicker in Fragment?

后端 未结 4 1199
梦毁少年i
梦毁少年i 2020-12-25 14:39

I am new on Android Fragments and I want to know that how can I show or implement DatePicker in simple Fragmentand not a FragmentActivity

4条回答
  •  执念已碎
    2020-12-25 15:15

    First of all i can say that,Selected answer is working fine.it is a good approach. But if you want to use MaterailDatePicker with Fragment. I found a solution. Try following for Set DatePicker in Fragment.

    Gradle

    dependencies {
      compile 'com.wdullaer:materialdatetimepicker:3.1.3'
    }
    

    DatePickerFragment

    package com.wdullaer.datetimepickerexample;
    
    import android.app.Fragment;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.TextView;
    
    import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
    
    import java.util.Calendar;
    
    /**
     * A simple {@link Fragment} subclass.
     */
    public class DatePickerFragment extends Fragment implements DatePickerDialog.OnDateSetListener {
    
        private TextView dateTextView;
        private CheckBox modeDarkDate;
        private CheckBox modeCustomAccentDate;
        private CheckBox vibrateDate;
        private CheckBox dismissDate;
        private CheckBox titleDate;
        private CheckBox showYearFirst;
        private CheckBox showVersion2;
        private CheckBox limitSelectableDays;
        private CheckBox highlightDays;
    
        public DatePickerFragment() {
            // Required empty public constructor
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.datepicker_layout, container, false);
    
            // Find our View instances
            dateTextView = (TextView) view.findViewById(R.id.date_textview);
            Button dateButton = (Button) view.findViewById(R.id.date_button);
            modeDarkDate = (CheckBox) view.findViewById(R.id.mode_dark_date);
            modeCustomAccentDate = (CheckBox) view.findViewById(R.id.mode_custom_accent_date);
            vibrateDate = (CheckBox) view.findViewById(R.id.vibrate_date);
            dismissDate = (CheckBox) view.findViewById(R.id.dismiss_date);
            titleDate = (CheckBox) view.findViewById(R.id.title_date);
            showYearFirst = (CheckBox) view.findViewById(R.id.show_year_first);
            showVersion2 = (CheckBox) view.findViewById(R.id.show_version_2);
            limitSelectableDays = (CheckBox) view.findViewById(R.id.limit_dates);
            highlightDays = (CheckBox) view.findViewById(R.id.highlight_dates);
    
            // Show a datepicker when the dateButton is clicked
            dateButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Calendar now = Calendar.getInstance();
                    DatePickerDialog dpd = DatePickerDialog.newInstance(
                            DatePickerFragment.this,
                            now.get(Calendar.YEAR),
                            now.get(Calendar.MONTH),
                            now.get(Calendar.DAY_OF_MONTH)
                    );
                    dpd.setThemeDark(modeDarkDate.isChecked());
                    dpd.vibrate(vibrateDate.isChecked());
                    dpd.dismissOnPause(dismissDate.isChecked());
                    dpd.showYearPickerFirst(showYearFirst.isChecked());
                    dpd.setVersion(showVersion2.isChecked() ? DatePickerDialog.Version.VERSION_2 : DatePickerDialog.Version.VERSION_1);
                    if (modeCustomAccentDate.isChecked()) {
                        dpd.setAccentColor(Color.parseColor("#9C27B0"));
                    }
                    if (titleDate.isChecked()) {
                        dpd.setTitle("DatePicker Title");
                    }
                    if (highlightDays.isChecked()) {
                        Calendar date1 = Calendar.getInstance();
                        Calendar date2 = Calendar.getInstance();
                        date2.add(Calendar.WEEK_OF_MONTH, -1);
                        Calendar date3 = Calendar.getInstance();
                        date3.add(Calendar.WEEK_OF_MONTH, 1);
                        Calendar[] days = {date1, date2, date3};
                        dpd.setHighlightedDays(days);
                    }
                    if (limitSelectableDays.isChecked()) {
                        Calendar[] days = new Calendar[13];
                        for (int i = -6; i < 7; i++) {
                            Calendar day = Calendar.getInstance();
                            day.add(Calendar.DAY_OF_MONTH, i * 2);
                            days[i + 6] = day;
                        }
                        dpd.setSelectableDays(days);
                    }
                    dpd.show(getFragmentManager(), "Datepickerdialog");
                }
            });
    
            return view;
        }
    
        @Override
        public void onResume() {
            super.onResume();
            DatePickerDialog dpd = (DatePickerDialog) getFragmentManager().findFragmentByTag("Datepickerdialog");
            if(dpd != null) dpd.setOnDateSetListener(this);
        }
    
        @Override
        public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
            String date = "You picked the following date: "+dayOfMonth+"/"+(++monthOfYear)+"/"+year;
            dateTextView.setText(date);
        }
    }
    

提交回复
热议问题