DialogFragment实现DatePicker

本文介绍了如何使用DialogFragment创建一个DatePicker。首先,需要创建DialogFragment并重写onCreateDialog,XML布局仅包含DatePicker。然后,使用AlertDialog.Builder构建对话框,并在活动里调用DialogFragment。最后,为DialogFragment添加动画效果,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上效果图
这里写图片描述

要实现这个效果,首先,我们得先创建一个Dialogfragment,注意我们只重写onCreateDialog函数,其他的函数就不用管了,在此之前我们先写好xml文件,很简单,只需要把datepicker作为根布局就可以了

datepicker.xml

<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/datePicker"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:calendarViewShown="false">
</DatePicker>
我们需要在onCreateDialog返回一个dialog,这个dialog我们一般都使用AlertDialog,现在官方已经不推荐使用Dialog了,具体代码如下:
View v = LayoutInflater.from(getActivity())
                .inflate(R.layout.datepicker, null);
        final DatePicker datePicker = (DatePicker)v;
        datePicker.setMinDate(TimeUtil.getDayLong());
        //设置可选择的最小日期,TimeUtil是我自己写的工具类,返回当前日期的long值
        AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(getActivity())
                .setView(datePicker)
                .setTitle(R.string.datepicker_title)
                .setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {            //添加自己的代码
                        }
                    }
                })
                .setNegativeButton(R.string.button_cancel, null);
                 return dialogbuilder.create();

alertdialog的构造用到了十分典型的Builder模式,这个模式可以使我们更容易组织alerdialog。

写完了dialogfragment之后,就可以在活动中调用它了,代码十分简单
 FragmentManager manager = getSupportFragmentManager(); 
 DatePickerFragment datePickerFragment = new DatePickerFragment();
 //datePickerFragment即为我们写的继承dialogfragment的组件
 datePickerFragment.setActitityType(activityType);
 datePickerFragment.show(manager, "DatePickerFragment");
不过这个dialogfragment显示的方式可能有些难看,这里给出我的一个动画的实现方式
if (dialog.getWindow() != null) {
            Window window=dialog.getWindow();
            window.setWindowAnimations(R.style.dialogAnim);
        }

动画的xml代码如下:

<style name="dialogAnim" mce_bogus="1" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/dialog_push_in</item>
        <item name="android:windowExitAnimation">@anim/dialog_push_out</item>
    </style>

dialog_push_in

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="@android:integer/config_shortAnimTime"
        android:fromYScale="0.5"
        android:fromXScale="0.4"
        android:toXScale="1.0"
        android:toYScale="1.0"
        />

    <alpha
        android:duration="@android:integer/config_shortAnimTime"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

dialog_push_out

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="@android:integer/config_shortAnimTime"
        android:fromYScale="1.0"
        android:fromXScale="1.0"
        android:toXScale="0.4"
        android:toYScale="0.5"
        />

    <alpha
        android:duration="@android:integer/config_shortAnimTime"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值