Android Kotlin 實(shí)現(xiàn)底部彈框日歷組件的案例
需求
如下圖所示, 底部彈出日歷組件
原生插件使用的有一個(gè)好處是可以根據(jù)你的系統(tǒng)語言切換插件內(nèi)的語言, 剛好我們這個(gè)app面向的國外用戶, 而產(chǎn)品對(duì)日歷組件的日期顯示有特別要求, 因此這無疑減少了我們切換語言的開發(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">
// 這個(gè)是最關(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
/**
* 個(gè)人信息頁
*/
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)外部點(diǎn)擊事件
popupWindow.isOutsideTouchable = true
// 設(shè)置PopupWindow是否能響應(yīng)點(diǎn)擊事件
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 實(shí)現(xiàn)底部彈框日歷組件的文章就介紹到這了,更多相關(guān)Android Kotlin底部彈框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android通知欄增加快捷開關(guān)的功能實(shí)現(xiàn)教程
對(duì)于Android來說其中一項(xiàng)很方便的操作便是下拉菜單,下拉菜單欄可以快捷打開某項(xiàng)設(shè)置,這篇文章主要給大家介紹了關(guān)于Android通知欄增加快捷開關(guān)的功能實(shí)現(xiàn),需要的朋友可以參考下2023-01-01
Android List(集合)中的對(duì)象以某一個(gè)字段排序案例
這篇文章主要介紹了Android List(集合)中的對(duì)象以某一個(gè)字段排序案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Android自定義View實(shí)現(xiàn)微信支付密碼輸入框
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)微信支付密碼輸入框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
解決Android SearchView不顯示搜索icon的問題
這篇文章主要介紹了解決Android SearchView不顯示搜索icon問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Android入門之PopupWindow用法實(shí)例解析
這篇文章主要介紹了Android入門之PopupWindow用法,對(duì)于Android初學(xué)者來說有一定的學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下2014-08-08

