日期选择器:如何弹出日期选择器时点击按钮,存储值变量 [英] Datepicker: How to popup datepicker when click on button and store value in variable

查看:174
本文介绍了日期选择器:如何弹出日期选择器时点击按钮,存储值变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示日期选择器弹出窗口。我已经发现了一些例子,但我没有得到它正确。我有一个按钮,我想,当我点击按钮的日期选择器对话框应弹出设定日期后,该日期应存放在一个变量。请为我提供样品code或良好的关系。

I want to show datepicker popup window. I have found some examples but i am not getting it properly. I have one button and i want that when i click on button the datepicker dialog should popup and after setting the date, the date should be stored in a variable. PLease provide me sample code or good links.

推荐答案

请这个类的内部类中的活性

Make this class as the inner class in your activity

如果你想设置当前日期,当你打开你的日期选择器,你看到的日期。你可以把它像这样

if you wanna set the current date as the date you see when you open your datePicker. You can get it like this

Calendar c = Calendar.getInstance();
int startYear = c.get(Calendar.YEAR);
int startMonth = c.get(Calendar.MONTH);
int startDay = c.get(Calendar.DAY_OF_MONTH);

class StartDatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        // Use the current date as the default date in the picker
        DatePickerDialog dialog = new DatePickerDialog(BookingFormActivity.this, this, startYear, startMonth, startDay);
 return dialog;

    }
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        // Do something with the date chosen by the user
        startYear = year;
        startMonth = monthOfYear;
        startDay = dayOfMonth;
        updateStartDateDisplay();


    }
    } 

您可以通过设置此方法的onClick调用该对话框

you can call this dialog by setting this method to onClick

  public void showStartDateDialog(View v){
    DialogFragment dialogFragment = new StartDatePicker();
    dialogFragment.show(getFragmentManager(), "start_date_picker");
    }

这篇关于日期选择器:如何弹出日期选择器时点击按钮,存储值变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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