Kotlin –简单的倒数计时器

时间:2019-01-08 16:27:57

标签: android kotlin countdowntimer

我知道如何在Java中创建一个简单的20秒倒数计时器。但是我想在Kotlin中创建这个。

package android.os;

new CountDownTimer(20000, 1000) {
    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
    }
    public void onFinish() {
        mTextField.setText("Time's finished!");
    }
}.start();

如何使用Kotlin做到这一点?

5 个答案:

答案 0 :(得分:2)

您可以使用Kotlin对象:

val timer = object: CountDownTimer {
    override fun onTick(millisUntilFinished: Long) {...}

    override fun onFinish() {...}
}
timer.start()

答案 1 :(得分:1)

尝试使用这样的对象:

var countDownTimer = object : CountDownTimer(2000, 1000) {
    // override object functions here, do it quicker by setting cursor on object, then type alt + enter ; implement members
}

访问此网站:https://try.kotlinlang.org/#/Kotlin%20Koans/Introduction/Java%20to%20Kotlin%20conversion/Task.kt

右上角有一个小按钮“从Java转换”,可能对您有用。

编辑:

不要忘记在需要时通过在声明末尾或活动/片段中的任意位置添加.start()来启动该对象:

countDownTimer.start()

答案 2 :(得分:1)

可以将计时器设置为倒数计时,在我看来,这是最简单的方法。

在布局xml中添加Chronometer视图,例如

<Chronometer  
 android:id="@+id/view_timer"   
 tools:targetApi="24"  
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"/>

然后在您的活动或片段中:

   view_timer.isCountDown = true
   view_timer.base = SystemClock.elapsedRealtime() + 20000
   view_timer.start()

答案 3 :(得分:1)

我已经解决了Kotlin中的计时器问题,


<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:fillViewport="true"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:paddingLeft="@dimen/marginActivity"
            android:orientation="vertical">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    style="@style/textInfoDate"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="@string/date_cmd" />

                <TextView
                    android:id="@+id/date_cmd"
                    android:layout_marginLeft="20dp"
                    style="@style/tvInfoDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    style="@style/textInfoDate"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="@string/date_liv_souhaite_ISIS" />

                <TextView
                    android:id="@+id/date_souhait_isis"
                    android:layout_marginLeft="20dp"
                    style="@style/tvInfoDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView

                    style="@style/textInfoDate"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="@string/date_liv_souhaite_Dis" />

                <TextView
                    android:id="@+id/date_souhait_dist"
                    android:layout_marginLeft="20dp"
                    style="@style/tvInfoDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    style="@style/textInfoDate"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="@string/date_MADA_previ" />
                <TextView
                    android:id="@+id/date_mada_previ"
                    android:layout_marginLeft="20dp"
                    style="@style/tvInfoDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    style="@style/textInfoDate"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="@string/date_MADA_engag" />

                <TextView
                    android:id="@+id/date_mada_engag"
                    android:layout_marginLeft="20dp"
                    style="@style/tvInfoDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    style="@style/textInfoDate"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="@string/date_MADC" />

                <TextView
                    android:id="@+id/date_madc"
                    android:layout_marginLeft="20dp"
                    style="@style/tvInfoDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
            </LinearLayout>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>


我已经将协程用作计时器。

答案 4 :(得分:1)

如果您想以天时分分秒显示倒计时

private lateinit var countDownTimer:CountDownTimer
.
.
.
    fun printDifferenceDateForHours() {

            val currentTime = Calendar.getInstance().time
            val endDateDay = "03/02/2020 21:00:00"
            val format1 = SimpleDateFormat("dd/MM/yyyy hh:mm:ss",Locale.getDefault())
            val endDate = format1.parse(endDateDay)

            //milliseconds
            var different = endDate.time - currentTime.time
            countDownTimer = object : CountDownTimer(different, 1000) {

                override fun onTick(millisUntilFinished: Long) {
                    var diff = millisUntilFinished
                    val secondsInMilli: Long = 1000
                    val minutesInMilli = secondsInMilli * 60
                    val hoursInMilli = minutesInMilli * 60
                    val daysInMilli = hoursInMilli * 24

                    val elapsedDays = diff / daysInMilli
                    diff %= daysInMilli

                    val elapsedHours = diff / hoursInMilli
                    diff %= hoursInMilli

                    val elapsedMinutes = diff / minutesInMilli
                    diff %= minutesInMilli

                    val elapsedSeconds = diff / secondsInMilli

                    txt_timeleft.text = "$elapsedDays days $elapsedHours hs $elapsedMinutes min $elapsedSeconds sec"
                }

                override fun onFinish() {
                    txt_timeleft.text = "done!"
                }
            }.start()
        }

如果要导航到其他活动/片段,请确保取消倒计时

countDownTimer.cancel()

代码输出

  

51天17时56分5秒