Kotlin 中的 Age In Minutes 应用程序

37 篇文章 0 订阅
16 篇文章 0 订阅

设置用户界面

  • 为了设计应用程序的 UI,我们将使用线性布局

  • 线性布局的方向将是垂直的

  • 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@color/backgroundColor"
    android:padding="16dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="CALCULATE YOUR"
        android:textColor="@color/textColor"
        android:textSize="25sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="AGE"
        android:padding="10dp"
        android:textColor="@color/white"
        android:textSize="25sp"
        android:background="@color/ageTextBackgroundColor"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="IN MINUTES"
        android:textColor="@color/textColor"
        android:textSize="25sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnDatePicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#FFFFFF"
        android:textColor="#929292"
        android:text="Select Date"
        android:layout_marginTop="15dp"
        android:textStyle="bold"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/tvSelectedDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#844046"
        android:layout_marginTop="20dp"
        android:textSize="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/lightGreyTextColor"
        android:text="Selected Date"
        android:textSize="18sp"/>

    <TextView
        android:id="@+id/tvSelectedDateInMinutes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#844046"
        android:layout_marginTop="20dp"
        android:textSize="35sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/lightGreyTextColor"
        android:text="Age in minutes"
        android:textSize="18sp"/>


</LinearLayout>

编写应用程序的逻辑

首先,我们将从布局中传递对 btnDatePicker 的引用,并将 onclicklistener 设置为 btnDatePicker,我们将在其中调用“clickDatePicker()”函数

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btnDatePicker: Button = findViewById(R.id.btnDatePicker)

        btnDatePicker.setOnClickListener {
             //call clickDatePicker when this button is clicked
            clickDatePicker()
        }
    }

打开日期选择器对话框的功能

这获取使用默认时区和语言环境的日历。返回的日历以默认时区的当前时间为准。

fun clickDatePicker(){

        val myCalendar = Calendar.getInstance()
        val year = myCalendar.get(Calendar.YEAR)
        val month = myCalendar.get(Calendar.MONTH)
        val day = myCalendar.get(Calendar.DAY_OF_MONTH)

        //get the id of the textviews from the layout
        val tvSelectedDate:TextView = findViewById(R.id.tvSelectedDate)
        val tvSelectedDateInMinutes:TextView = findViewById(R.id.tvSelectedDateInMinutes)

打开日期选择器对话框:

  • DatePickerDialog 使用父上下文的默认日期选择器对话框主题为指定日期创建一个新的日期选择器对话框。用于指示用户已完成日期选择的侦听器。
  • 这里选择的日期设置为格式即:日/月/年,并且在 Kotlin 中计算的月份是 0 到 11,因此我们需要添加 +1 以便它可以作为选择。
val dpd = DatePickerDialog(this,{ _, selectedYear, selectedMonth, selectedDayOfMonth ->

            val selectedDate = "$selectedDayOfMonth/${selectedMonth+1}/$selectedYear"
            // Selected date it set to the TextView to make it visible to user.
            tvSelectedDate.text = selectedDate
  • 在这里,我们采用了 Date Formatter 的实例,因为它将以我们将其作为参数和 Locale 传递的格式格式化我们选择的日期。在这里,我将格式传递为 dd/MM/yyyy。
  • 格式化程序会将选定的日期解析为 Date 对象,因此我们可以简单地将日期以毫秒为单位。
val sdf = SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)
val theDate = sdf.parse(selectedDate)

以分钟为单位计算年龄的公式:

  • 在这里,我们从 Date 对象中获得了以毫秒为单位的时间,正如我们所知的公式,毫秒可以通过将其除以 1000 转换为秒,秒可以通过将其除以 60 转换为分钟。

  • 将安全调用运算符与 .let 一起使用,以避免应用程序崩溃 currentDate 为 null。

  • System.currentTimeMillis() 设置自 1970 年 1 月 1 日 00:00:00 时区以来此支持的最大日期(以毫秒为单位)。

 theDate?.let {

                 val selectedDateInMinutes = theDate.time/60000
                // Here we have parsed the current date with the Date Formatter which is used above.
                val currentDate = sdf.parse(sdf.format(System.currentTimeMillis()))
                //use the safe call operator with .let to avoid app crashing it currentDate is null
                currentDate?.let {
                    // Current date in to minutes.
                    val currentDateToMinutes = currentDate.time/60000
                    val differenceInMinutes = currentDateToMinutes - selectedDateInMinutes
                    tvSelectedDateInMinutes.text = differenceInMinutes.toString()
                }
            }
        },
        year,month,day)

限制用户选择今天和未来的一天:

  • 86400000 是 24 小时的毫秒数。用于限制用户选择今天和未来的一天。
dpd.datePicker.maxDate = System.currentTimeMillis() - 86400000
 dpd.show()

输出截图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值