简体   繁体   English

Android Datepicker:如何在单击按钮时弹出日期选择器并将值存储在变量中

[英]Android Datepicker: How to popup datepicker when click on button and store value in variable

I'm trying to solve the same problem described here . 我正在尝试解决此处描述的相同问题。 After the date has been chosen I wish to use it to open a browser. 选择日期后,我希望使用它来打开浏览器。 It should also be noted that I'm doing this work from a fragment. 还应该注意的是,我是从一个片段开始的。

I am using the second solution but am facing two problems: 我正在使用第二种解决方案,但面临两个问题:

  1. When I press the ChooseDate button the DatePicker pops up but before I can choose the date the app already opens the browser with the old date. 当我按下ChooseDate按钮时,会弹出DatePicker,但在选择日期之前,应用程序已经使用旧日期打开了浏览器。 When I press back button I see the DatePicker and I can choose a date, and if I press the ChooseDate button again then the browser opens with almost (see 2) the desired date, and then pressing back I still see the DatePicker and so on. 当我按下“后退”按钮时,我看到了DatePicker,我可以选择一个日期,如果再次按下“ ChooseDate”按钮,则浏览器几乎打开(请参见2)所需的日期,然后按回车,我仍然看到DatePicker,依此类推。
  2. The DatePicker returns the month with an offset of -1. DatePicker返回偏移量为-1的月份。 EG I choose March and I get February. 例如,我选择“三月”,然后选择“二月”。 It happens both on the Emulator and on a Nexus 5 device. 它在模拟器和Nexus 5设备上均会发生。 mCalender is a GregorianCalendar. mCalender是GregorianCalendar。

My questions: 我的问题:

  1. How can I get the Calendar to stay until I choose a date and then make it go away? 我如何才能保留日历,直到选择一个日期然后将其取消?

  2. Did anyone else encounter the wrong month supplied by the Calender (I rebuilt the App, closed and opened Android Studio), and if so, what did you do with this (other then the obvious +1 hack) 其他人是否遇到过日历日历错误的月份(我重新构建了该应用程序,关闭并打开了Android Studio),如果是,那么您对此做了什么(除了显而易见的+1 hack)

I am using Android Studio 1.1.0, API 21. 我正在使用Android Studio 1.1.0,API 21。

Thank you! 谢谢!

Code: 码:

        mChooseDateButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            openBrowser(CHOOSE_DAY);
        }
    });

void openBrowser(int option) {

    String date = "";

    switch (option) {
        ...

        case CHOOSE_DAY:
            chooseDate();
            date = mChosenDate;
            break;

        ...
    }


    String url = getUrl(date);

    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(browserIntent);
}


...

void chooseDate() {

    int year = mCalendar.get(Calendar.YEAR);
    int month = mCalendar.get(Calendar.MONTH);
    int day = mCalendar.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog dialog = new DatePickerDialog(this.getActivity(),
            new DateSetListener(), year, month, day);
    dialog.show();
}

class DateSetListener implements DatePickerDialog.OnDateSetListener {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
        // TODO Auto-generated method stub
        int chosenMonth = monthOfYear;
        int chosenDay = dayOfMonth;
        Log.i("DEBUG","chosen month: " + chosenMonth);
        updateChosenDate(chosenMonth,chosenDay);
    }
}

You need to move this: 您需要移动此:

String url = getUrl(date);

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);

to the method the DateSetListener calls onDateSet, so it doesn't fire before you've actually set a date. DateSetListener调用onDateSet方法,因此在您实际设置日期之前不会触发。 Your current code would have it run through the switch case and start the intent without waiting on the Dialog in openBrowser(). 您当前的代码将使它在switch的情况下运行并启动意图,而无需等待openBrowser()中的Dialog。

The issue with the month could be because the calendar begins at 0 and the value being supplied is not offsetting this. 与月份有关的问题可能是因为日历从0开始,并且提供的值没有抵消它。 So just increment by 1. 因此只需增加1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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