《Android编程权威指南(第三版)》第十二章挑战练习Demo上

《Android编程权威指南(第三版)》第十二章挑战练习Demo上

因为第十二章有两个挑战练习,所以分成上下来发,先说第一个,题目要求在CrimeFragment中加一个按钮显示TimePickerFragment视图界面,当然,只显示肯定很简单,但我们可以自己加点难度,就像日期按钮一样,在点开日历之后选择日期返回后也可以更改CrimeFragment里面的数值,比如:

                

可以看到在recyclerview里面的时间原本是16:39,最后更改为了21:00,实现原理和书上实现日期更改代码相同,最好自己跟着在写一遍,加深印象,我就放一下更改过的代码和添加的代码:

CrimeFragment.java:

/**
         * 这里其实和上面的日期按钮差不多的,就不过多赘述了
         */
        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;
        }
        /**
         * 更改了一下书上的代码,用switch语句来进行选择
         */
        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:(这一块和DatePickerFragment差不多,有些许地方不太一样)

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

}

注意,Date的getHour等方法已经过时,可以使用calendar.get(Calendar.HOUR_OF_DAY)来获得小时数

源码地址:Demo

许久没写:特来注明,最近事情较多,会马上开始补上

你可能感兴趣的:(Android)