如何获取日期时间选择器的值?

时间:2017-09-15 03:44:05

标签: java android datetime datetimepicker

我必须从警报对话框设置日期和日期。我创建了一个新的XML布局来获取Date Time Picker的值。但是我不知道如何在每个Textview上获得不同的价值。

MainFragment extends Fragment implements
        DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {

    int day, month, year, hour, minute;
    int dayFinal, monthFinal, yearFinal, hourFinal, minuteFinal;
    String dayFinalString, monthFinalString, yearFinalString, hourFinalString, minuteFinalString;

    TextView et2;

    String dateFrom;
    String Dates, time;

    @Override
    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
        yearFinal = i;
        monthFinal = i1 + 1;
        dayFinal = i2;

        Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);

        Dates = (monthFinal < 10 ? ("0" + monthFinal) : (monthFinal)) + "-" + (dayFinal < 10 ? ("0" + dayFinal) : (dayFinal)) + "-" +  yearFinal ;

        TimePickerDialog timePickerDialog = new TimePickerDialog(_context, this, hour, minute, DateFormat.is24HourFormat(_context));
        timePickerDialog.show();
    }

    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        hourFinal = i;
        minuteFinal = i1;

        time = (hourFinal < 10 ? ("0" + hourFinal) : (hourFinal)) + ":" + (minuteFinal < 10 ? ("0" + minuteFinal) : (minuteFinal));



                            //Here I want to set for example
                            et2.setText(Dates + " " + time + ":00");
                            dateFromListFragment = et2.getText().toString();

                            et3.setText(Dates + " " + time + ":00");
                            dateFromListFragment = et3.getText().toString();


    }

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

       ....

            @Override
            public void onClick(View view) {

                final Dialog dialog = new Dialog(getContext());

                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.date_time);
                dialog.setCancelable(false);

                ImageView close = (ImageView) dialog.findViewById(R.id.close);
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });

                et2 = (TextView) dialog.findViewById(R.id.editText2);
                et2.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View arg0) {
                        Calendar c = Calendar.getInstance();
                        year = c.get(Calendar.YEAR);
                        month = c.get(Calendar.MONTH);
                        day = c.get(Calendar.DAY_OF_MONTH);

                        DatePickerDialog datePickerDialog = new DatePickerDialog(_context, SnailTrailTwoHrsFragment.this, year, month, day);
                        datePickerDialog.show();

                    }
                });

                final TextView et3 = (TextView) dialog.findViewById(R.id.editText3);
                et3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Calendar c = Calendar.getInstance();
                        year = c.get(Calendar.YEAR);
                        month = c.get(Calendar.MONTH);
                        day = c.get(Calendar.DAY_OF_MONTH);

                        DatePickerDialog datePickerDialog = new DatePickerDialog(_context, SnailTrailTwoHrsFragment.this, year, month, day);
                        datePickerDialog.show();

                    }
                });

}

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="30dp"
        android:weightSum="1">

        <ImageView
            android:id="@+id/close"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:layout_margin="5dp"
            android:layout_marginBottom="15dp"
            android:src="@drawable/close"
            android:textSize="20dp" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentEnd="false"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/close"
        android:gravity="right"
        android:paddingLeft="25dp"
        android:paddingRight="0dp" >

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date From: "></TextView>

        <TextView
            android:id="@+id/editText2"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:paddingTop="25dp"
            android:text="________________________________"
            android:textAllCaps="true"
            android:textColor="@color/colorBlack" />
    </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="horizontal"
            android:paddingTop="10dp"
            android:id="@+id/linearLayout3"
            android:layout_below="@+id/linearLayout"
            android:layout_marginBottom="25dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:paddingLeft="35dp"
            android:paddingRight="0dp" >

            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date To: " />

            <TextView
                android:id="@+id/editText3"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:paddingTop="25dp"
                android:text="________________________________"
                android:textAllCaps="true"
                android:textColor="@color/colorBlack" />

        </LinearLayout>

        <ImageView
            android:id="@+id/button5"
            android:layout_width="66dp"
            android:layout_height="33dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="135dp"
            app:srcCompat="@drawable/next"
            android:layout_below="@+id/linearLayout3" />

    </RelativeLayout>

当我按DateTimePickerDate From TextView相同时,我希望在对话框中设置Date To TextView

问题:当我按下Date From TextView时,我无法获得Date Time Picker的值,并设置为Date FromDate To相同。

1 个答案:

答案 0 :(得分:0)

 private boolean IsSelectBirthDate = false;
 DateFormat myDateFormat = new SimpleDateFormat("dd-MM-yyyy");
 public final int DATE_PICKER_BIRTHDAY_ID = 112;
 public final int DATE_PICKER_ANNIVERSARY_ID = 113;
 private int myYear;
private int myMonth;
private int myDay;

 myImageViewBirthdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                IsSelectBirthDate = true;
                showDialog(DATE_PICKER_BIRTHDAY_ID);
            }
        });

        myImageViewAnniversary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                IsSelectBirthDate = false;
                showDialog(DATE_PICKER_ANNIVERSARY_ID);
            }
        });

            /////////////////////////////////////////

          @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_PICKER_BIRTHDAY_ID: {
                Calendar c = Calendar.getInstance();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog dialog1 = new DatePickerDialog(this, pickerListener, year, month, day);
                dialog1.getDatePicker().setMaxDate(new Date().getTime());
                return dialog1;

            }
            case DATE_PICKER_ANNIVERSARY_ID: {
                Calendar c = Calendar.getInstance();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog dialog2 = new DatePickerDialog(this, pickerListener, year, month, day);
                dialog2.getDatePicker().setMaxDate(new Date().getTime());
                return dialog2;
            }
        }
        return null;
    }

    /////////////////////////////////////////
        private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {

            myYear = selectedYear;
            myMonth = selectedMonth;
            myDay = selectedDay;

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_MONTH, myDay);
            cal.set(Calendar.MONTH, myMonth);
            cal.set(Calendar.YEAR, myYear);
            Date selectedDate = cal.getTime();

            String strSelectedDate = "";
            try {
                strSelectedDate = myDateFormat.format(selectedDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (IsSelectBirthDate) {
                myEditTextBirthDate.setText(strSelectedDate);
            } else {
                myEditTextAnniversary.setText(strSelectedDate);
            }

        }
    };