在textview中设置选定的日期

时间:2018-04-18 05:42:53

标签: java android

问题是每当我从DatePiker中选择日期时,它会在TextView显示当前日期

如何在TextView中设置所选日期?

如果我打开此活动,如何设置动态日期,然后在textview中自动设置。

在Code下面打开一个DatePiker对话框:

case R.id.llDueDate:
                new DateTimePicker(this,null, startDateListner).showDateTimePicker();
                break;

DateTimePiker.java

public class DateTimePicker  {
    public Activity mActivity;
    private Date mDate;
    private Calendar calendar;
    private int year, month, day, hour, min;
    private DateTimeSelectedListner listner;

    public interface DateTimeSelectedListner {
        public void onDateTimeSelected(Date date);
    }

    public DateTimePicker(Activity activity, Date date, DateTimeSelectedListner listner) {
        this.mActivity = activity;
        this.listner = listner;
        this.calendar = Calendar.getInstance();
        if(mDate != null){
            this.mDate = mDate;
            calendar.setTime(mDate);
        } else {
            mDate = calendar.getTime();
        }
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        min = calendar.get(Calendar.MINUTE);
    }
    public void showDateTimePicker(){


        View v = mActivity.getLayoutInflater().inflate(R.layout.dialog_datetime, null);
        DatePicker datePicker = (DatePicker) v.findViewById(R.id.datePicker);
        TimePicker timePicker = (TimePicker) v.findViewById(R.id.timePicker);

        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            public void onDateChanged(DatePicker view, int year, int month, int day) {
                DateTimePicker.this.year = year;
                DateTimePicker.this.month = month;
                DateTimePicker.this.day = day;
                updateDateTime();
            }
        });

        timePicker.setCurrentHour(hour);
        timePicker.setCurrentMinute(min);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            public void onTimeChanged(TimePicker view, int hour, int min) {
                DateTimePicker.this.hour = hour;
                DateTimePicker.this.min = min;
                updateDateTime();
            }
        });

        new AlertDialog.Builder(mActivity)
                .setView(v)
                .setTitle(R.string.date_picker_title)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        listner.onDateTimeSelected(mDate);
                    }
                })
                .create().show();
    }
    public void showDatePicker(){
        View v = mActivity.getLayoutInflater().inflate(R.layout.dialog_date, null);
        DatePicker datePicker = (DatePicker) v.findViewById(R.id.datePicker);

        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            public void onDateChanged(DatePicker view, int year, int month, int day) {
                DateTimePicker.this.year = year;
                DateTimePicker.this.month = month;
                DateTimePicker.this.day = day;
                updateDateTime();
            }
        });

        new AlertDialog.Builder(mActivity)
                .setView(v)
                .setTitle(R.string.title_select_date_of_birth)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        listner.onDateTimeSelected(mDate);
                    }
                })
                .create().show();
    }
    public void updateDateTime() {
        mDate = new GregorianCalendar(year, month, day, hour, min).getTime();
    }
}

以下代码是在 TextView

中设置日期
public DateTimePicker.DateTimeSelectedListner startDateListner = new DateTimePicker.DateTimeSelectedListner() {
        @Override
        public void onDateTimeSelected(Date date) {
            txtDateTime.setText(Util.getInstance(AddTodoActivity.this).convertDateToddMMMyyyyhmma(date));
        }
    };

ConvertDateToddMMyyy 功能:

public String convertDateToddMMMyyyyhmma(Date mDate) {
        String outputPattern = "dd-MMM-yyyy h:mm a";
        SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
        String str = outputFormat.format(mDate);
        return str;
    }

此对话框始终为当前日期而非选定日期,因此我错过了某些代码或错误

2 个答案:

答案 0 :(得分:0)

试试这段代码:

    final Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog dp = new DatePickerDialog(Activity.this,                  new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year,
                              int monthOfYear, int dayOfMonth) {
            textView.setText(dayOfMonth + "-" 
                  + (monthOfYear + 1) + "-" + year);
        }
    }, year, month, day);
    dp.show();

答案 1 :(得分:0)

更新dateTimePicker类

public  class DateTimePicker {
    public Activity mActivity;
    private Date mDate;
    private Calendar calendar;
    private int year, month, day, hour, min;
    private DatePickerDialog.OnDateSetListener listner;
    private DatePickerDialog datePickerDialog;

    public interface DateTimeSelectedListner {
        public void onDateTimeSelected(Date date);
    }

    public DateTimePicker(Activity activity, Date date, DatePickerDialog.OnDateSetListener listner, Calendar calendar) {
        this.mActivity = activity;
        this.listner = listner;
        this.calendar = calendar;
        if (mDate != null) {
            this.mDate = date;
            calendar.setTime(mDate);
        } else {
            mDate = calendar.getTime();
        }
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        min = calendar.get(Calendar.MINUTE);
    }

    public void showDateTimePicker() {
        datePickerDialog = new DatePickerDialog(mActivity, listner, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.setButton(DatePickerDialog.BUTTON_POSITIVE, "Done", datePickerDialog);
        datePickerDialog.show();
    }

//        public void showDatePicker() {
//            View v = mActivity.getLayoutInflater().inflate(R.layout.dialog_date, null);
//            DatePicker datePicker = (DatePicker) v.findViewById(R.id.datePicker);
//
//            datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
//                public void onDateChanged(DatePicker view, int year, int month, int day) {
//                    DateTimePicker.this.year = year;
//                    DateTimePicker.this.month = month;
//                    DateTimePicker.this.day = day;
//                    updateDateTime();
//                }
//            });
//
//            new AlertDialog.Builder(mActivity)
//                    .setView(v)
//                    .setTitle("selected dob")
//                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
//                        public void onClick(DialogInterface dialog, int which) {
//                            listner.onDateTimeSelected(mDate);
//                        }
//                    })
//                    .create().show();
//        }

    public void updateDateTime() {
        mDate = new GregorianCalendar(year, month, day, hour, min).getTime();
    }


}

在班级中创建归档变量

  public DatePickerDialog.OnDateSetListener startDateListner;
    private Calendar calendar;

在oncreate of activity中调用此方法

 private void initializezDatePickerAndCalendar() {

        calendar = Calendar.getInstance();
        startDateListner = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // TODO Auto-generated method stub

                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, monthOfYear);
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                setSelectedDate(calendar);
            }

        };

    }

单击按钮显示Datepicker

时调用此方法
 private void showDatePicker() {
        new DateTimePicker(this, null, startDateListner, calendar).showDateTimePicker();

    }

在您的活动中也添加此方法

  private void setSelectedDate(Calendar myCalendar) {

        String myFormat = "dd-MMM-yyyy h:mm a"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        String selectedDAte = sdf.format(myCalendar.getTime());
        Toast.makeText(this, selectedDAte, Toast.LENGTH_LONG).show();

        // here you can set date on textview


    }