当前位置: 代码网 > it编程>App开发>Android > Android Kotlin 实现底部弹框日历组件的案例

Android Kotlin 实现底部弹框日历组件的案例

2024年08月02日 Android 我要评论
需求如下图所示, 底部弹出日历组件原生插件使用的有一个好处是可以根据你的系统语言切换插件内的语言, 刚好我们这个app面向的国外用户, 而产品对日历组件的日期显示有特别要求, 因此这无疑减少了我们切换

需求

如下图所示, 底部弹出日历组件
原生插件使用的有一个好处是可以根据你的系统语言切换插件内的语言, 刚好我们这个app面向的国外用户, 而产品对日历组件的日期显示有特别要求, 因此这无疑减少了我们切换语言的开发工作量

代码

1. custom_bottom_datepicker.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="wrap_content"
    android:background="#98adabab">
    <relativelayout
        android:id="@+id/dialog_bottom"
        android:layout_width="match_parent"
        android:layout_height="320dp"
        android:layout_alignparentbottom="true"
        android:layout_centerhorizontal="true"
        android:background="@drawable/rounded_button_white"
        android:padding="20dp">
        <linearlayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginbottom="40dp">
            <textview
                android:id="@+id/cancel"
                android:layout_width="0dp"
                android:layout_weight="1.5"
                android:layout_height="38dp"
                android:layout_gravity="center_horizontal"
                android:text="cancel"
                android:textallcaps="false"
                android:textcolor="@color/colorprimary"
                android:textsize="14sp"
                android:gravity="center"
                />
            <textview
                android:id="@+id/title"
                android:layout_width="0dp"
                android:layout_weight="5"
                android:layout_height="38dp"
                android:layout_gravity="center_horizontal"
                android:text="date of birth"
                android:textallcaps="false"
                android:textcolor="@color/colorprimary"
                android:textsize="16sp"
                android:gravity="center"
                />
            <textview
                android:id="@+id/save"
                android:layout_width="0dp"
                android:layout_weight="1.5"
                android:layout_height="38dp"
                android:layout_gravity="center_horizontal"
                android:layout_margintop="139dp"
                android:text="save"
                android:textallcaps="false"
                android:gravity="center"
                android:textcolor="@color/colorprimary"
                android:textsize="14sp" />
        </linearlayout>
        <linearlayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margintop="40dp"
            android:gravity="center_horizontal"
            android:orientation="vertical">
            <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="5dp">
				// 这个是最关键的
                <datepicker
                    android:id="@+id/dp_date"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:calendarviewshown="false"
                    android:datepickermode="spinner"
                    />
            </linearlayout>
        </linearlayout>
    </relativelayout>
</relativelayout>

2. activity.kt

import java.util.date
/**
 * 个人信息页
 */
class personnalinfoactivity : appcompatactivity() {
    private var year: string? = null
    private var month: string? = null
    private var day: string? = null
    @requiresapi(build.version_codes.n)
    override fun oncreate(savedinstancestate: bundle?) {
        super.oncreate(savedinstancestate)
        setcontentview(r.layout.activity_personal_info)
        birthday.setonclicklistener {
            choosebirthfun()
        }
    }
    private fun choosebirthfun() = run {
        val userinfo = sharedprefs.loaduserfrompreferences(this@personnalinfoactivity)
        val view = layoutinflater.inflate(r.layout.custom_bottom_datepicker, null)
        val popupwindow = popupwindow(
            view,
            viewgroup.layoutparams.match_parent,
            viewgroup.layoutparams.match_parent,
            true
        )
        // 设置popupwindow是否能响应外部点击事件
        popupwindow.isoutsidetouchable = true
        // 设置popupwindow是否能响应点击事件
        popupwindow.istouchable = true
        // 设置popupwindow弹出窗体的背景
        popupwindow.setbackgrounddrawable(
            colordrawable(
                contextcompat.getcolor(
                    this,
                    android.r.color.transparent
                )
            )
        )
        // 设置popupwindow从底部弹出
        popupwindow.showatlocation(view, gravity.bottom, 0, 0)
        val save = view.findviewbyid<textview>(r.id.save)
        val cancel = view.findviewbyid<textview>(r.id.cancel)
        val dpdate = view.findviewbyid<datepicker>(r.id.dp_date);
        // 设置默认日期
        if (userinfo != null) {
            if (userinfo.birthday == "") {
                dpdate.init(1970, 0, 1, null);
            } else {
                year?.let {
                    (month)?.let { it1 ->
                        day?.let { it2 ->
                            dpdate.init(
                                it.toint(),
                                (it1.toint()) - 1,
                                it2.toint(),
                                null
                            )
                        }
                    }
                };
            }
        }
        save.setonclicklistener {
            if (popupwindow.isshowing) {
                year = dpdate.getyear().tostring()
                month = ((dpdate.getmonth() + 1).tostring())
                day = dpdate.getdayofmonth().tostring()
                val birthstr = "${year}-${month}-${day}"
                // 此处可以处理选择好的日期
                popupwindow.dismiss()
            }
        }
        cancel.setonclicklistener {
            if (popupwindow.isshowing) {
                popupwindow.dismiss()
            }
        }
    }
}

完成~

到此这篇关于android kotlin 实现底部弹框日历组件的文章就介绍到这了,更多相关android kotlin底部弹框内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com