Я пытаюсь использовать таймер для обновления текстового представления, используя список строк.Используя этот таймер, я могу напечатать элементы в списке 1 на 1, однако текстовое представление переходит прямо к последнему элементу.Кто-нибудь может указать правильный способ обновления текстового представления с использованием таймера, чтобы каждое обновление отображалось независимо при запуске приложения?
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>