提取日期Datepicker Android

时间:2016-01-08 21:55:35

标签: java android date datepicker android-dialogfragment

我一直在尝试使用Datepicker来提取我所得到的日期here当我尝试使用我创建的功能时问题就出现了( getDate),我无法从activity调用该方法来获取字符串,因为这些方法无法解决。谁能解释我为什么?

这是我活动中Datepicker的代码。

public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {
    private String date;
    @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 year, int month, int day) {
         date=Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(day);
    }

    public String getDate(){
        return date;
    }
}


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

1 个答案:

答案 0 :(得分:0)

最后,我在这篇文章Android - Date Picker - Obatin Value上找到了一个可能的解决方案,特别是这个评论:

declare Textview as public & static & call it on dateSet method by-className.textviewName.setText();

但我仍然不明白为什么我不能做我最初想要的事情