Android - Date Picker - Obatin Value

风格不统一 提交于 2020-01-05 07:58:41

问题


I am using a DatePicker Dialog in my activity for the user to select a Date. What I want to do is to display the selected date in a textView. Can anyone please help me on how to obtain the selected date and then display it accordingly. Thanks in advance!

Following is the code i'm using to implement the DatePicker Dialog:

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 yyyy, int mm, int dd) {
            // Do something with the date chosen by the user


            }
}

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}   

回答1:


in onDateSet method,

String cuurent_date = String.valueOf(c.get(Calendar.DAY_OF_MONTH))
            + "/" + String.valueOf(c.get(Calendar.MONTH) + 1) + "/"
            + String.valueOf(c.get(Calendar.YEAR));



回答2:


Implement a Listener Interface in your activity and pass it to the DatePickerFragment. Then trigger the listener by calling it's methods. It's described here: http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents



来源:https://stackoverflow.com/questions/16140306/android-date-picker-obatin-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!