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

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

问题描述

我想显示日期选择器弹出窗口.我找到了一些例子,但我没有正确理解.我有一个按钮,我希望当我点击按钮时,日期选择器对话框应该弹出,设置日期后,日期应该存储在一个变量中.请给我提供示例代码或好的链接.

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

如果您想将当前日期设置为您打开 datePicker 时看到的日期.你可以这样弄

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天全站免登陆