如何从日期/时间选择器对话框中获取日期和时间并显示它?

时间:2017-07-24 14:58:30

标签: java android date time fragment

        public class MainActivity extends FragmentActivity {

        TextView dateView, timeView;
        String date;

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

            dateView = (TextView) findViewById(R.id.dateTextID);
            timeView = (TextView) findViewById(R.id.timeTextID);

        }

        public void showDatePickerDialog(View v) {
            DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getFragmentManager(), "datePicker");
        }

        public void showTimePickerDialog(View v) {
            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getFragmentManager(), "timePicker");
        }

        //*******************************************//
        //DATE PICKER CLASS
        public static class DatePickerFragment extends DialogFragment
                implements DatePickerDialog.OnDateSetListener {

            @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(), (DatePickerDialog.OnDateSetListener)getActivity(), year, month, day);
            }

            public void onDateSet(DatePicker view, int year, int month, int day) {
                // Do something with the date chosen by the user
                String date =Integer.toString(day)+"/"+Integer.toString(month)+"/"+Integer.toString(yeat);
                dateView.setText(date);
            }
        }
        //*******************************************//
        //TIME PICKER CLASS
        public static class TimePickerFragment extends DialogFragment
                implements TimePickerDialog.OnTimeSetListener {

            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                // Use the current time as the default values for the picker
                final Calendar c = Calendar.getInstance();
                int hour = c.get(Calendar.HOUR_OF_DAY);
                int minute = c.get(Calendar.MINUTE);

                // Create a new instance of TimePickerDialog and return it
                return new TimePickerDialog(getActivity(), this, hour, minute,
                        DateFormat.is24HourFormat(getActivity()));
            }

            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                // Do something with the time chosen by the user
            }
        }

    }

我想设置dateView和timeView(都是TextView)来显示用户选择的日期和时间,但问题是我在onDateSet和onTimeSet方法中无法做任何事情,因为它们是静态的。

在onDateSet()中:

String date = Integer.toString(day)+“/”+ Integer.toString(month)+“/”+ Integer.toString(yeat);                     dateView.setText(日期);

这给出了无法从静态上下文引用非静态字段dateView的错误。怎么解决这个问题。我查找了类似的问题,但它们都只是DatePickerFragment而不是同一个类中的日期和时间。 这是我的布局XML:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showDatePickerDialog"
        android:text="@string/date_button_set"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:id="@+id/dateTextID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/date_selected"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="54dp"
        android:layout_marginStart="54dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showTimePickerDialog"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignStart="@+id/button1"
        android:layout_below="@+id/button1"
        android:text="@string/time_button_set" />

    <TextView
        android:id="@+id/timeTextID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button"
        android:layout_alignLeft="@+id/dateTextID"
        android:layout_alignStart="@+id/dateTextID" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

使用界面将设定的日期和时间传达回活动。

public class MainActivity extends FragmentActivity implements
    DatePickerFragment.DatePickedListener, TimePickerFragment.TimePickedListener {

    TextView dateView, timeView;
    String date;

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

        dateView = (TextView) findViewById(R.id.dateTextID);
        timeView = (TextView) findViewById(R.id.timeTextID);

    }

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "timePicker");
    }

    @Override
    public void onDatePicked(String date) {
        dateView.setText(date);
    }

    @Override
    public void onTimeSet(String time) {
        timeView.setText(time);
    }
}

DatePicker类

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    private static final String TAG = "DatePickerFragment";

    private SimpleDateFormat dateFormatterEntry = new SimpleDateFormat("M/dd/yyyy");
    private Calendar calendar;
    int year;
    int month;
    int day;

    private DatePickedListener listener;

    public static interface DatePickedListener {
        void onDatePicked(String date);
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Log.i(TAG, "onCreateDialog");

        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);

        listener = (DatePickedListener) getActivity();

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        calendar.set(year, month, day, 0, 0);
        String formattedDate = dateFormatterEntry.format(calendar.getTime());
        Log.i(TAG, formattedDate);
        listener.onDatePicked(formattedDate);
    }
}

对TimePicker执行相同操作并添加界面

public class TimePickerFragment extends DialogFragment
            implements TimePickerDialog.OnTimeSetListener {

        private TimePickedListener listener;

        public static interface TimePickedListener {
            void onTimePicked(String time);
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current time as the default values for the picker
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            // Create a new instance of TimePickerDialog and return it
            return new TimePickerDialog(getActivity(), this, hour, minute,
                    DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            listener.onDatePicked(Integer.toString(hourOfDay));
        }
    }

答案 1 :(得分:0)

您可以定义静态TextView(我知道它会导致内存泄漏)和onDateSet方法获取Date并将其设置为textview

答案 2 :(得分:0)

时间选择器

final Calendar getDate = Calendar.getInstance();
    TimePickerDialog timePickerDialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            getDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
            getDate.set(Calendar.MINUTE, minute);
            SimpleDateFormat timeformat=new SimpleDateFormat("HH:mm a");
            timeFormat.format(getDate.getTime()));
        }
    }, getDate.get(Calendar.HOUR_OF_DAY), getDate.get(Calendar.MINUTE), false);
    timePickerDialog.show()

日期选择器

final Calendar getDate = Calendar.getInstance();
    DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            getDate.set(year, monthOfYear, dayOfMonth);
       SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy");
                format.format(getDate.getTime());
        }
    }, getDate.get(Calendar.YEAR),
            getDate.get(Calendar.MONTH),
            getDate.get(Calendar.DAY_OF_MONTH));
    datePickerDialog.show();