Android的 - 日期选取器 - Obatin价值 [英] Android - Date Picker - Obatin Value

查看:207
本文介绍了Android的 - 日期选取器 - Obatin价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个datepicker对话在我的用户活动选择一个日期。
我想要做的是显示一个TextView选定的日期。任何人都可以请帮我如何获得所选择的日期,然后相应地显示它。
在此先感谢!

以下是code我使用来实现的DatePicker对话框:

 公共静态类DatePickerFragment扩展DialogFragment
实现DatePickerDialog.OnDateSetListener {        @覆盖
            公共对话框onCreateDialog(捆绑savedInstanceState){
                //使用当前日期作为选择器的默认日期
                最后的日历C = Calendar.getInstance();
                    年整型= c.get(Calendar.YEAR);
                    INT月= c.get(的Calendar.MONTH);
                    INT天= c.get(Calendar.DAY_OF_MONTH);                    //创建DatePickerDialog的新实例,并将其返回
                    返回新DatePickerDialog(getActivity(),这,年,月,日);
            }            公共无效onDateSet(查看的DatePicker,诠释YYYY,诠释毫米,诠释DD){
            //做一些与用户所选择的日期
            }
}公共无效showDatePickerDialog(视图v){
    DialogFragment newFragment =新DatePickerFragment();
    newFragment.show(getFragmentManager(),日期选择器);
}


解决方案

在onDateSet方法,

 字符串cuurent_date =将String.valueOf(c.get(Calendar.DAY_OF_MONTH))
            +/+将String.valueOf(c.get(的Calendar.MONTH)+ 1)+/
            +将String.valueOf(c.get(Calendar.YEAR));

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");
}   

解决方案

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));

这篇关于Android的 - 日期选取器 - Obatin价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆