【问题标题】:extract date Datepicker Android提取日期 Datepicker Android
【发布时间】:2016-01-08 21:55:34
【问题描述】:

我一直在尝试提取使用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();
}

【问题讨论】:

    标签: java android date datepicker android-dialogfragment


    【解决方案1】:

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

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

    但我还是不明白为什么我不能做我最初想做的事情

    【讨论】:

    • DialogFragment 没有 getDate 方法,但 DatePickerFragment 有那个方法。
    • getDate 方法是我实现的,我试图调用所以 DatePicker 和 DialogFragment 都没有那个方法。
    • 哦,我忘了。我想现在你知道为什么 getDate 不可访问了。 ;-)
    猜你喜欢
    • 1970-01-01
    • 2016-02-03
    • 2017-02-20
    • 1970-01-01
    • 2019-12-22
    • 2016-03-02
    • 2011-02-01
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多