使用日期选择器将日期设置为 editText

Setting Date to editText using Date picker

我想使用 datePickerDialog 将两个不同的日期设置为两个不同的 EditText。我正在使用两个不同的字符串对象来存储日期,然后分别将它们设置在两个不同的 EditText 上,但问题是两个 EditText 都采用相同的值。

protected Dialog onCreateDialog(int id) {

    // Get the calander
    java.util.Calendar c = java.util.Calendar.getInstance();

    // From calander get the year, month, day, hour, minute
    int year = c.get(java.util.Calendar.YEAR);
    int month = c.get(java.util.Calendar.MONTH);
    int day = c.get(java.util.Calendar.DAY_OF_MONTH);

    switch (id) {
        case Date_from:

            // Open the datepicker dialog
            return new DatePickerDialog(WastageRecordsByDateActivity.this, date_listener, year,
                    month, day);
        case Date_to:

            return new DatePickerDialog(WastageRecordsByDateActivity.this, date_listener, year,
                    month, day);
    }
    return null;
}

// Date picker dialog
DatePickerDialog.OnDateSetListener date_listener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        // store the data in one string and set it to edittext
        String date1 = String.valueOf(day) + "-" + String.valueOf(month + 1)
                + "-" + String.valueOf(year);
        editTextFrom.setText(date1);

        String date2 = String.valueOf(day) + "-" + String.valueOf(month + 1)
                + "-" + String.valueOf(year);
        editTextTo.setText(date2);
    }
};

}

发生这种情况是因为您的两个字符串对象都具有相同的数据,这些数据来自方法。你所做的只是将数据保存在不同的对象上,但数据是相同的。

 String date1 = String.valueOf(day) + "-" + String.valueOf(month + 1)
                + "-" + String.valueOf(year);
        editTextFrom.setText(date1);

 String date2 = String.valueOf(day) + "-" + String.valueOf(month + 1)
                + "-" + String.valueOf(year);
        editTextTo.setText(date2);

这是因为您为两个 editText 设置了相同的值,如下所示,

        String date1 = String.valueOf(day) + "-" + String.valueOf(month + 1)
                + "-" + String.valueOf(year);
        editTextFrom.setText(date1);

        String date2 = String.valueOf(day) + "-" + String.valueOf(month + 1)
                + "-" + String.valueOf(year);
        editTextTo.setText(date2);

由于我们没有你的完整源代码,但你可以在这里尝试一个小补丁,你可以传递另一个 arg 布尔值,

    switch (id) {
            case Date_from:

                // Open the datepicker dialog
                return new DatePickerDialog(WastageRecordsByDateActivity.this, date_listener, year,
                        month, day, true);
            case Date_to:

                return new DatePickerDialog(WastageRecordsByDateActivity.this, date_listener, year,
                        month, day, false);
        }
        return null;
    }

    // Date picker dialog
    DatePickerDialog.OnDateSetListener date_listener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int month, int day, Boolean stat) {
            // store the data in one string and set it to edittext
            if(stat==true){
                String date1 = String.valueOf(day) + "-" + String.valueOf(month + 1)
                    + "-" + String.valueOf(year);
                editTextFrom.setText(date1);
            }
            if(stat==false){
                 String date2 = String.valueOf(day) + "-" + String.valueOf(month + 1)
                    + "-" + String.valueOf(year);
                 editTextTo.setText(date2);
            }
        }
    };

试试这个

switch (id) {
        case Date_from:

            // Open the datepicker dialog
            return new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String date1 = String.valueOf(day) + "-" + String.valueOf(month + 1)
                + "-" + String.valueOf(year);
        editTextFrom.setText(date1);
                        }
                    }, 2017, 04, 25);
        case Date_to:

            return new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                            String date2 = String.valueOf(day) + "-" + String.valueOf(month + 1)
                + "-" + String.valueOf(year);
                         editTextTo.setText(date2);
                        }
                    }, 2017, 04, 25);
    }

试试这个,让我知道兄弟,

    edtxt1a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(edtxt1a.getWindowToken(), 0);

                showDispatchCalendar();

            }
        });


        private void showDispatchCalendar() {
        Calendar c1 = Calendar.getInstance();
        DatePickerDialog da = new DatePickerDialog(getActivity(), mDateSetListener, sYearIni, sMonthIni, sDayIni);
        c1.add(DATE, 0);
        Date newDate = c1.getTime();
        da.getDatePicker().setMinDate(newDate.getTime());

        da.show();

    }

private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    mYearIni = year;
                    mMonthIni = monthOfYear;
                    mDayIni = dayOfMonth;

                    colocar_fecha();
                }
            };


            private void colocar_fecha() {

        day1 = mDayIni;
        month1 = mMonthIni + 1;
        year1 = mYearIni;
        edtxt2a.setEnabled(true);
        edtxt2a.setBackgroundResource(R.drawable.rounded_border_edittext);
        edtxt1a.setText(day1 + "-" + month1 + "-" + year1 + " ");
    }