简体   繁体   中英

Getting data from DatePickerDialog to TextView

Hello, I need help.

I have MainActivity , PickerDialogs and DateSettings and I want get date to EditText from DatePickerDialog . How can I do it? Thanks very much.

Here is java classes:

DateSettings.java

public class DateSettings implements DatePickerDialog.OnDateSetListener
{

    Context context;

    public DateSettings(Context context)
    {
        this.context = context;
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
    {
        Toast.makeText(context, "Selected date: " +monthOfYear+ " / " +dayOfMonth+ " / " +year, Toast.LENGTH_SHORT).show();
    }
}

PickerDialogs.java

public class PickerDialogs extends DialogFragment
{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        DateSettings dateSettings = new DateSettings(getActivity());

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

        DatePickerDialog date_dialog;
        date_dialog = new DatePickerDialog(getActivity(), dateSettings, year, month, day);
        return date_dialog;
    }
}

MainActivity.java

public class MainActivity extends FragmentActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void setDate(View view)
    {
        PickerDialogs pickerDialogs = new PickerDialogs();
        pickerDialogs.show(getSupportFragmentManager(), "date picker?");
    }


    public void setTime(View view)
    {
        PickerDialogsTime pickerDialogsTime = new PickerDialogsTime();
        pickerDialogsTime.show(getSupportFragmentManager(), "time_picker");
    }
}

Your DialogFragment does not have a method or interface to return the picked date to the caller,See the below code

Here - Inside the constructor I am passing an OnDateSetListener object mDateSetListener so that i can return the result to the activity

public class DatePickerDialogFragment extends DialogFragment {

    private Context context;
    private Calendar MinDate, MaxDate;
    private OnDateSetListener mDateSetListener;


    public DatePickerDialogFragment() {
    }

    public DatePickerDialogFragment(OnDateSetListener callback, Calendar MinDate, Calendar MaxDate, Context context) {
        mDateSetListener = callback;
        this.MinDate = MinDate;
        this.MaxDate = MaxDate;
        this.context = context;
    }
    public DatePickerDialogFragment(OnDateSetListener callback, Context context) {
        mDateSetListener = callback;
        this.context = context;
    }
    DatePickerDialog dd;
    DatePicker dp;

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar cal = Calendar.getInstance();

        dd = new DatePickerDialog(getActivity(), this.mDateSetListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
        dd.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                try {
                    if (MinDate!=null&&MaxDate!=null) {
                        ((DatePickerDialog) dialog).getDatePicker().setMaxDate(MaxDate.getTimeInMillis());
                        ((DatePickerDialog) dialog).getDatePicker().setMinDate(MinDate.getTimeInMillis());

                    }
                } catch (NullPointerException e) {
                    dialog.dismiss();
                    e.printStackTrace();
                }
            }
        });
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        return dd;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

}

And when you create the DialogFragmen,Do it like:

Update Use this inside the activity of fragment where you want your datepicker to return value to,Note that you will get the date value inside the onDateSet function

dialogFragment = new DatePickerDialogFragment(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar cal = GregorianCalendar.getInstance();
                cal.set( year, monthOfYear, dayOfMonth);
                String currentDateandTime = sdf.format( cal.getTime());
                et_date.setText(currentDateandTime);
            }
        }, context);

So that the picked date is returned to the callback method and set to the TextView et_date

Show it when needed like

dialogFragment.show(getFragmentManager(), "Date");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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