"The Definitive Guide to Android Programming (Third Edition)" Chapter 12 Challenge Exercise Demo

"The Definitive Guide to Android Programming (Third Edition)" Chapter 12 Challenge Exercise Demo

Because there are two challenging exercises in Chapter 12, it is divided into upper and lower parts. Let’s start with the first one. The title requires adding a button to the CrimeFragment to display the TimePickerFragment view interface. Of course, it is definitely very simple to only display it, but we can make it more difficult by ourselves. , just like the date button, you can also change the value in the CrimeFragment after selecting the date after clicking the calendar, for example:

                

It can be seen that the time in the recyclerview was originally 16:39, and finally changed to 21:00. The implementation principle is the same as the date change code in the book. It is best to write it yourself to deepen the impression. I will put the changed one. Code and added code:

CrimeFragment.java:

/**
         * This is actually similar to the date button above, so I won't go into details.
         */
        updateTime();
        mTimeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fragmentManager = getFragmentManager();
                TimePickerFragment dialog = TimePickerFragment.newInstance(mCrime.getDate());
                dialog.setTargetFragment(CrimeFragment.this, REQUEST_TIME);
                dialog.show(fragmentManager, DIALOG_TIME);
            }
        });
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        /**
         * Changed the code in the book, use switch statement to select
         */
        switch (requestCode) {
            case REQUEST_DATE:
                Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
                mCrime.setDate(date);
                updateDate();
                break;
            case REQUEST_TIME:
                Date date1 = (Date) data.getSerializableExtra(TimePickerFragment.EXTRA_TIME);
                mCrime.setDate(date1);
                updateTime();
            default:
                break;
        }
    }

TimePickerFragment.java: (This piece is similar to DatePickerFragment, with some differences)

public class TimePickerFragment extends DialogFragment {

    public static final String EXTRA_TIME = "com.bignerdranch.android.criminalintent.time";
    private static final String ARG_TIME = "time";

    private TimePicker mTimePicker;

    public static TimePickerFragment newInstance(Date date) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_TIME, date);
        TimePickerFragment fragment = new TimePickerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceStance) {
        Date date = (Date) getArguments().getSerializable(ARG_TIME);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_time, null);

        mTimePicker = (TimePicker) view.findViewById(R.id.dialog_time_picker);
        mTimePicker.setHour (hours);
        mTimePicker.setMinute(minute);

        return new AlertDialog.Builder(getActivity())
                .setView(view)
                .setTitle(R.string.time_picker_title)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int hour = mTimePicker.getHour();
                        int minute = mTimePicker.getMinute();
                        Date date = new GregorianCalendar(0, 0, 0, hour, minute).getTime();
                        sendResult(Activity.RESULT_OK, date);
                    }
                })
                .create();
    }

    private void sendResult(int resultCode, Date date) {
        if (getTargetFragment() == null) {
            return;
        }
        Intent intent = new Intent();
        intent.putExtra(EXTRA_TIME, date);

        getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);
    }

}

Note that methods such as Date's getHour are outdated, you can use calendar.get(Calendar.HOUR_OF_DAY) to get the hours

Source address: Demo

I haven't written for a long time: I have come to note that there have been a lot of things happening recently, and I will start to make up soon.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325586920&siteId=291194637