Android Kotlin 實(shí)現(xiàn)底部彈框日歷組件的案例
需求
如下圖所示, 底部彈出日歷組件
原生插件使用的有一個(gè)好處是可以根據(jù)你的系統(tǒng)語(yǔ)言切換插件內(nèi)的語(yǔ)言, 剛好我們這個(gè)app面向的國(guó)外用戶(hù), 而產(chǎn)品對(duì)日歷組件的日期顯示有特別要求, 因此這無(wú)疑減少了我們切換語(yǔ)言的開(kāi)發(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è)人信息頁(yè)
*/
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)文章
詳解用RxJava實(shí)現(xiàn)事件總線(Event Bus)
本篇文章主要介紹了用RxJava實(shí)現(xiàn)事件總線(Event Bus),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
使用TransitionDrawable實(shí)現(xiàn)多張圖片淡入淡出效果
這篇文章主要為大家詳細(xì)介紹了使用TransitionDrawable實(shí)現(xiàn)多張圖片淡入淡出效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
從源代碼分析Android Universal ImageLoader的緩存處理機(jī)制
這篇文章主要介紹了從源代碼分析Android Universal ImageLoader的緩存處理機(jī)制 的相關(guān)資料,需要的朋友可以參考下2016-01-01
實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件
AutoCompleteTextView組件被用在輸入框中能實(shí)現(xiàn)輸入內(nèi)容自動(dòng)補(bǔ)全的功能,類(lèi)似于大家平時(shí)用Google時(shí)的輸入聯(lián)想,這里我們來(lái)用實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件,特別是實(shí)現(xiàn)郵箱地址補(bǔ)全的例子,非常實(shí)用2016-05-05
AndroidQ分區(qū)存儲(chǔ)權(quán)限變更及適配的實(shí)現(xiàn)
這篇文章主要介紹了AndroidQ分區(qū)存儲(chǔ)權(quán)限變更及適配的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

