简体   繁体   English

Android日期选择器片段无法设置为自定义日期

[英]Android Date Picker Fragment can not be set to Custom date

In here 'onClickListner' on 'Image Button' my custom 'DatePickerFragment' is displaying to select the date. 在此处的“图像按钮”上的“ onClickListner”中,我的自定义“ DatePickerFragment”正在显示以选择日期。 Below is the code for 下面是代码

DatePickerFragment.java DatePickerFragment.java

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.DatePicker;

import java.util.Calendar;

/**
 * Created by Admin on 9/4/18.
 */
public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    private onDatePickerListener mListener;
    private boolean future;

    public DatePickerFragment(){

    }

//    public DatePickerFragment(boolean future){
//        this.future=future;
//    }

    @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);

        Log.d("DATE_PICKER", " CURRENT_MONTH " + month);

        int day = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.AppBaseTheme,this, year, month, day);

        if(isFuture() ==false){
            datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
        }
        // Create a new instance of DatePickerDialog and return it
        return datePickerDialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        callListener(year, month, day);
    }

    public DialogFragment setCallbackListener(onDatePickerListener listener) {
        mListener = listener;
        return null;
    }

    private void callListener(int year, int month, int day) {
        if (mListener != null) mListener.onDataSet(year, month, day);
    }

    public boolean isFuture() {
        return future;
    }

    public void setFuture(boolean future) {
        this.future = future;
    }

    public interface onDatePickerListener {
        void onDataSet(int year, int month, int day);
    }
}

Then I call this 'Fragment' in my 'MainActivity' to set the date to 'EditText' when click on the 'OK' button from the 'DatePickerFragment'. 然后,当我从“ DatePickerFragment”中单击“确定”按钮时,在我的“ MainActivity”中将此“片段”设置为“ EditText”。

MainActivity.java MainActivity.java

import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import com.sample.DatePickerFragment;

public class AddInterestedKarmaActivity extends AppCompatActivity {   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_interested_karma);

        ButterKnife.bind(this);
        context = this;

        EditText dateEdittext = (Edittext)findViewById(R.id.date_edit);
        setDate(dateEditText);

    }    

    private void setDate(final EditText dateEditText){

            datePickerFragment.setCallbackListener(new DatePickerFragment.onDatePickerListener() {
                @Override
                public void onDataSet(int year, int month, int day) {

                    int currentMonth = 0;

                    if (month == 0) {
                        currentMonth = 1;
                    } else if (month == 1) {
                        currentMonth = 2;
                    } else if (month == 2) {
                        currentMonth = 3;
                    } else if (month == 3) {
                        currentMonth = 4;
                    } else if (month == 4) {
                        currentMonth = 5;
                    } else if (month == 5) {
                        currentMonth = 6;
                    } else if (month == 6) {
                        currentMonth = 7;
                    } else if (month == 7) {
                        currentMonth = 8;
                    } else if (month == 8) {
                        currentMonth = 9;
                    } else if (month == 9) {
                        currentMonth = 10;
                    } else if (month == 10) {
                        currentMonth = 11;
                    } else if (month == 11) {
                        currentMonth = 12;
                    } else if (month == 12) {

                    }

                    dateEditText.setText(day + "/" + currentMonth + "/" + year);

                }
            });

        }

        DialogFragment datePicker = datePickerFragment;

        datePicker.show(getSupportFragmentManager(), "datePicker");
    }

    private void setTime(final EditText timeEditText){

        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        TimePickerDialog timePickerDialog = new TimePickerDialog(context,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay,
                                          int minute) {

                        String curTime = String.format("%02d:%02d", hourOfDay, minute);
                        timeEditText.setText(curTime);
                    }
                }, mHour, mMinute, false);

        timePickerDialog.show();
    }
}

I need to set the Date of 'DatePickerFragmenet' for a custom date (not today). 我需要将“ DatePickerFragmenet”的日期设置为自定义日期(不是今天)。 such as set the 'DatePickerFragment' onCreate() to the mentioned above final int y, m, d values that from 'dateEditText'. 例如将'DatePickerFragment'onCreate()设置为上述来自'dateEditText'的最终int,m,d值。 And also I need to set the custom 'time' to the 'TimePickerDialog' such as 'DatePickerFragment'. 而且我还需要将自定义“时间”设置为“ TimePickerDialog”,例如“ DatePickerFragment”。

There is one function in DatePickerDialog to set custom date( updateDate(year_, month_, day_) ) DatePickerDialog有一个函数可以设置自定义日期( updateDate(year_, month_, day_)

According to your code you need to make changes in your DatePickerFragment in onCreateDialog of DatePickerFragment where you are getting date , month and year from calendar, instead of getting that from Calendar provide your custom dates. 根据您的代码,您需要在DatePickerFragmentonCreateDialogDatePickerFragment中进行更改,从日历中获取datemonthyear ,而不是从Calendar中获取提供自定义日期。

Updated class is below 更新的课程如下

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    private onDatePickerListener mListener;
    private boolean future;
    int day_;
    int month_;
    int year_;
    private DatePickerDialog datePickerDialog;


    public DatePickerFragment() {

    }


    @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);

        //    Log.d("DATE_PICKER", " CURRENT_MONTH " + month);

        //   int day = c.get(Calendar.DAY_OF_MONTH);
        datePickerDialog = new DatePickerDialog(getActivity(), R.style.AppTheme, this, year_, month_, day_);

        if (isFuture() == false) {
            datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
        }
      //  datePickerDialog.updateDate(year_, month_, day_);

        // Create a new instance of DatePickerDialog and return it
        return datePickerDialog;
    }


    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        callListener(year, month, day);
    }

    public DialogFragment setCallbackListener(onDatePickerListener listener) {
        mListener = listener;
        return null;
    }

    private void callListener(int year, int month, int day) {
        if (mListener != null) mListener.onDataSet(year, month, day);
    }

    public boolean isFuture() {
        return future;
    }

    public void setFuture(boolean future) {
        this.future = future;
    }

    public interface onDatePickerListener {
        void onDataSet(int year, int month, int day);
    }
}

In your activity, you need to provide custom date. 在您的活动中,您需要提供自定义日期。 Updated Activity is below, you can add your custom date. 更新的活动如下,您可以添加自定义日期。

public class AddInterestedKarmaActivity extends AppCompatActivity {

    DatePickerFragment datePickerDialog;
    EditText dateEdittext;
    String customDate = "12/12/2007"; //dd/mm/yy

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_interested_karma);


        dateEdittext = (EditText) findViewById(R.id.date_edit);
        //  setDate(dateEditText);


        datePickerDialog = new DatePickerFragment();
        datePickerDialog.day_ = getDay();
        datePickerDialog.month_ = getMonth();
        datePickerDialog.year_ = getYear();

        datePickerDialog.setCallbackListener(new DatePickerFragment.onDatePickerListener() {
                                                 @Override
                                                 public void onDataSet(int year, int month, int day) {
                                                     month = month + 1;
                                                     dateEdittext.setText(day + "/" + month + "/" + year);

                                                 }
                                             }
        );

        datePickerDialog.show(getSupportFragmentManager(), "datePicker");

    }

    private int getYear() {
        return Integer.parseInt(changeDateFormat(customDate, "MM/dd/yyyy", "YYYY"));

    }

    private int getMonth() {
        return Integer.parseInt(changeDateFormat(customDate, "MM/dd/yyyy", "MM")) - 1;  //substract one from month because month gets start from 0 in Calendar.

    }

    private int getDay() {
        return Integer.parseInt(changeDateFormat(customDate, "MM/dd/yyyy", "dd"));
    }


    public String changeDateFormat(String dateString, String sourceDateFormat, String targetDateFormat) {
        if (dateString == null || dateString.isEmpty())
            return "";

        SimpleDateFormat inputDateFromat = new SimpleDateFormat(sourceDateFormat, Locale.US);
        Date date = new Date();

        try {
            date = inputDateFromat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat outputDateFormat = new SimpleDateFormat(targetDateFormat, Locale.US);

        return outputDateFormat.format(date);
    }


}

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

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