Android:使用计时器以Kotlin动态更新视图

时间:2018-06-06 00:31:16

标签: android timer kotlin

我正在尝试使用计时器来使用字符串列表更新文本视图。使用此计时器,我可以逐个打印列表中的项目,但文本视图会直接跳转到最后一项。任何人都可以指出使用计时器更新文本视图的正确方法,以便每个更新在应用程序运行时独立显示吗?

class MainActivity : AppCompatActivity() {

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


    val changingWord = findViewById<TextView>(R.id.changingWordTextView)
    var countDownTimer: CountDownTimer

    val titleShift = mutableListOf<String>("_____", "Words", "Verse", "Haiku", "Dream", "World", "Smile", "Story", "Glory", "Birth", "Plant", "Truth", "Voice", "Value", "Sound", "Human")

    fun WordChanger(num: Int) {

        countDownTimer = object : CountDownTimer(3000, 1000) { // starts at 3 seconds

            override fun onTick(secondsUntilDone: Long) {

            }

            override fun onFinish() {
                Log.i("We're done!", "No more countdown")
                changingWord.text = titleShift[num]
                println(titleShift[num])
            }
        }.start()

    }

    WordChanger(0)
    WordChanger(1)
    WordChanger(2)
    WordChanger(3)
    WordChanger(4)

}

}

这是XML:

<android.support.constraint.ConstraintLayout 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:background="@android:color/holo_green_light"
tools:context=".MainActivity">

<TextView
    android:id="@+id/changingWordTextView"
    android:layout_width="222dp"
    android:layout_height="96dp"
    android:fontFamily="@font/architects_daughter"
    android:text="______"
    android:textAppearance="@style/TextAppearance.AppCompat.Display2"
    android:textColor="@android:color/black"
    android:textSize="30sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.169"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.24" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="151dp"
    android:layout_height="22dp"
    android:layout_marginBottom="90dp"
    android:layout_marginEnd="7dp"
    android:layout_marginStart="93dp"
    android:fontFamily="@font/architects_daughter"
    android:textColor="@android:color/black"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/textView"
    app:layout_constraintTop_toBottomOf="@+id/haikusButton" />

<Button
    android:id="@+id/quotesButton"
    style="@android:style/Widget.Button.Inset"
    android:layout_width="90dp"
    android:layout_height="44dp"
    android:layout_marginBottom="7dp"
    android:layout_marginStart="45dp"
    android:layout_marginTop="276dp"
    android:text="Quotes"
    android:textColor="#000000"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toStartOf="@+id/haikusButton"
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/changingWordTextView" />

<Button
    android:id="@+id/poemsButton"
    style="@android:style/Widget.Button.Inset"
    android:layout_width="140dp"
    android:layout_height="53dp"
    android:layout_marginBottom="158dp"
    android:layout_marginEnd="136dp"
    android:layout_marginStart="135dp"
    android:layout_marginTop="121dp"
    android:text="Poems"
    android:textColor="#000000"
    app:layout_constraintBottom_toTopOf="@+id/textView2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/changingWordTextView"
    app:layout_constraintVertical_bias="0.0" />

<TextView
    android:id="@+id/titleTextView"
    android:layout_width="222dp"
    android:layout_height="96dp"
    android:fontFamily="@font/architects_daughter"
    android:text="Of The Day"
    android:textAppearance="@style/TextAppearance.AppCompat.Display2"
    android:textColor="@android:color/black"
    android:textSize="35sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.915"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.224" />

<Button
    android:id="@+id/haikusButton"
    style="@android:style/Widget.Button.Inset"
    android:layout_width="90dp"
    android:layout_height="44dp"
    android:layout_marginBottom="7dp"
    android:layout_marginEnd="46dp"
    android:layout_marginStart="140dp"
    android:layout_marginTop="286dp"
    android:text="Haikus"
    android:textColor="#000000"
    app:layout_constraintBottom_toTopOf="@+id/textView2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/quotesButton"
    app:layout_constraintTop_toBottomOf="@+id/titleTextView" />

<TextView
    android:id="@+id/textView"
    android:layout_width="141dp"
    android:layout_height="22dp"
    android:layout_marginBottom="90dp"
    android:layout_marginStart="20dp"
    android:fontFamily="@font/architects_daughter"
    android:textColor="@android:color/black"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/textView2"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/quotesButton" />

这是清单。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dnarial.blankoftheday">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data
        android:name="preloaded_fonts"
        android:resource="@array/preloaded_fonts" />
</application>

1 个答案:

答案 0 :(得分:1)

这是因为你是一次性启动所有计时器而不是你想要的一个一个。有几种方法可以做到这一点

  1. 只需使用一个CountDownTimer并根据已用完的时间更新onTick方法中的数据

    fun WordChanger(num: Int) {
    
    countDownTimer = object : CountDownTimer(3000 * <number of words>, 1000) { // starts at 3 seconds
    
        override fun onTick(secondsUntilDone: Long) {
            if (secondsUntilDone > 9000) {
                Log.i("We're done!", "No more countdown")
                changingWord.text = titleShift[0]
                println(titleShift[0])
            } else if (secondsUntilDone > 6000) {
                Log.i("We're done!", "No more countdown")
                changingWord.text = titleShift[1]
                println(titleShift[1])
            }
            .
            .
            .
            etc
        }
    
        override fun onFinish() {
        }
    }.start()}
    
  2. 如果你仍然坚持使用上述方法而不是你需要做的是提供时间作为输入。所以你的代码看起来像这样

    fun WordChanger(num: Int, time: Long) {
    
    val countDownTimer = object : CountDownTimer(time, 1000) { // starts at 3 seconds
    
        override fun onTick(secondsUntilDone: Long) {
    
        }
    
        override fun onFinish() {
            Log.i("We're done!", "No more countdown")
            changingWord.text = titleShift[num]
            println(titleShift[num])
        }
    }.start()}
    
     WordChanger(0, 3000) 
     WordChanger(1, 6000) 
     WordChanger(2, 9000) 
     WordChanger(3, 12000) 
     WordChanger(4, 15000)
    
  3. 如果您需要任何解释,请告诉我