设定发送短信的时间

时间:2020-06-28 09:08:51

标签: android viewmodel transformation countdowntimer android-viewmodel

我想创建一个倒计时计时器来发送短信

它可以编译并安装在模拟器上,但不显示任何内容

如果可以帮助解决我的问题,我会使用Google代码实验室示例及其链接

android-kotlin-fundamentals-apps是游戏片段

这是我的处理时间代码

class SMSViewModel: ViewModel() {

private val _currentTime = MutableLiveData<Long>()
val currentTime: LiveData<Long>
    get() = _currentTime

private val timer = object : CountDownTimer(COUNTDOWN_TIME, ONE_SECOND) {

    override fun onTick(millisUntilFinished: Long) {
        _currentTime.value = millisUntilFinished/ONE_SECOND
    }

    override fun onFinish() {
        _currentTime.value = DONE
    }
}.start()

// The String version of the current time
val currentTimeString = Transformations.map(currentTime) { time ->
    DateUtils.formatElapsedTime(time)
}

override fun onCleared() {
    super.onCleared()
    timer.cancel()
}

companion object{
    private const val DONE = 0L
    private const val ONE_SECOND = 1000L
    private const val COUNTDOWN_TIME = 180000L
}

和此XML文件

<layout
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">

<data>
    <variable
        name="SMSViewModel"
        type="com.example.panoramic.app.ui.singup.viewmodel.SMSViewModel" />
</data>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".app.ui.singup.SMSFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/timer_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:fontFamily="sans-serif"
            android:gravity="center"
            android:text="@{SMSViewModel.currentTimeString}"
            android:textColor="#000"
            android:textSize="30sp"
            android:textStyle="normal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="0:00" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

我肯定会在片段中初始化我的视图持有人

0 个答案:

没有答案