如何从“日期/时间选择器"对话框中获取日期和时间并显示它? [英] How to get date and time from Date/Time Picker dialog and show it?

查看:39
本文介绍了如何从“日期/时间选择器"对话框中获取日期和时间并显示它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        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方法中执行任何操作,因为它们是静态的.

I want to set dateView and timeView (both are TextView) to show date and time user selected but problem is that I can't do anything in onDateSet and onTimeSet methods because they are static.

在onDateSet()中:

In onDateSet():

字符串日期= Integer.toString(日)+"/" + Integer.toString(月)+"/" + Integer.toString(yeat);dateView.setText(date);

String date =Integer.toString(day)+"/"+Integer.toString(month)+"/"+Integer.toString(yeat); dateView.setText(date);

这会导致错误,即无法从静态上下文中引用非静态字段dateView.如何解决.我寻找了类似的问题,但它们都只是针对DatePickerFragment,而不是同一类中的日期和时间.这是我的布局XML:

This gives error that non static field dateView cannot be referenced from static context. How to solve that. I looked for similar questions but they are all for just DatePickerFragment not for both date and time in same class. Here is my XML for layout:

<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>

推荐答案

使用界面将设置的日期和时间传达给活动.

Use an interface to communicate the set date and time back to the activity.

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类

DatePicker class

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进行相同操作并添加界面

Do the same with TimePicker and add the interface

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

这篇关于如何从“日期/时间选择器"对话框中获取日期和时间并显示它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆