欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Kotlin 實現(xiàn)底部彈框日歷組件的案例

 更新時間:2024年08月02日 10:55:54   作者:代碼x手莓莓  
這篇文章主要介紹了Android Kotlin 實現(xiàn)底部彈框日歷組件的案例,本文通過實例代碼給大家介紹的非常詳細(xì),對大家大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

需求

如下圖所示, 底部彈出日歷組件
原生插件使用的有一個好處是可以根據(jù)你的系統(tǒng)語言切換插件內(nèi)的語言, 剛好我們這個app面向的國外用戶, 而產(chǎn)品對日歷組件的日期顯示有特別要求, 因此這無疑減少了我們切換語言的開發(fā)工作量

代碼

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">
				// 這個是最關(guān)鍵的
                <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
        )
        // 設(shè)置PopupWindow是否能響應(yīng)外部點擊事件
        popupWindow.isOutsideTouchable = true
        // 設(shè)置PopupWindow是否能響應(yīng)點擊事件
        popupWindow.isTouchable = true
        // 設(shè)置PopupWindow彈出窗體的背景
        popupWindow.setBackgroundDrawable(
            ColorDrawable(
                ContextCompat.getColor(
                    this,
                    android.R.color.transparent
                )
            )
        )
        // 設(shè)置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);
        // 設(shè)置默認(rèn)日期
        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()
            }
        }
    }
}

完成~

到此這篇關(guān)于Android Kotlin 實現(xiàn)底部彈框日歷組件的文章就介紹到這了,更多相關(guān)Android Kotlin底部彈框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android開心消消樂代碼實例詳解

    Android開心消消樂代碼實例詳解

    這篇文章主要介紹了Android開心消消樂代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Android通知欄增加快捷開關(guān)的功能實現(xiàn)教程

    Android通知欄增加快捷開關(guān)的功能實現(xiàn)教程

    對于Android來說其中一項很方便的操作便是下拉菜單,下拉菜單欄可以快捷打開某項設(shè)置,這篇文章主要給大家介紹了關(guān)于Android通知欄增加快捷開關(guān)的功能實現(xiàn),需要的朋友可以參考下
    2023-01-01
  • Android制作簡單的普通購物車

    Android制作簡單的普通購物車

    這篇文章主要介紹了Android制作簡單的普通購物車,利用ExpandabeListView制作購物車功能,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android獲取手機(jī)聯(lián)系人的方法

    Android獲取手機(jī)聯(lián)系人的方法

    這篇文章主要介紹了Android 獲取系統(tǒng)聯(lián)系人信息的實例的相關(guān)資料,希望通過本文大家能實現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-09-09
  • Android List(集合)中的對象以某一個字段排序案例

    Android List(集合)中的對象以某一個字段排序案例

    這篇文章主要介紹了Android List(集合)中的對象以某一個字段排序案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Android自定義View實現(xiàn)微信支付密碼輸入框

    Android自定義View實現(xiàn)微信支付密碼輸入框

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)微信支付密碼輸入框,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • 解決Android SearchView不顯示搜索icon的問題

    解決Android SearchView不顯示搜索icon的問題

    這篇文章主要介紹了解決Android SearchView不顯示搜索icon問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • Android日期選擇控件使用詳解

    Android日期選擇控件使用詳解

    這篇文章主要為大家詳細(xì)介紹了Android日期選擇控件的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 微信小程序 bnner滾動實例詳解

    微信小程序 bnner滾動實例詳解

    這篇文章主要介紹了微信小程序 bnner滾動實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Android入門之PopupWindow用法實例解析

    Android入門之PopupWindow用法實例解析

    這篇文章主要介紹了Android入門之PopupWindow用法,對于Android初學(xué)者來說有一定的學(xué)習(xí)借鑒價值,需要的朋友可以參考下
    2014-08-08

最新評論