Вот мой код.Я понял, как изменить одно текстовое поле при нажатии кнопки, но в ту минуту, когда я пытаюсь добавить еще один val, как это:
val tv = findViewById(R.id.questionfield) as TextView
tv.text = "What is your name?"
Я получаю сообщение об ошибке: «Конфликтующие объявления: val tv: Textview, val tv: TextView "
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="Avatar Response Field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/welcome" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:text="Question Pop Up Field"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:id="@+id/questionfield"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.497"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/button"/>
<Button
android:text="Click to Ask Alyssa a Question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView2"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
android:onClick="buttonClick"/>
<ImageView
android:layout_width="291dp"
android:layout_height="261dp" tools:src="@tools:sample/avatars"
android:id="@+id/imageView2" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/welcome"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/textView2"/>
</android.support.constraint.ConstraintLayout>
MainActivity.kt
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.TextView
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun buttonClick(v: View) {
val tv = findViewById(R.id.welcome) as TextView
tv.text = "Hi! I'm Alyssa"
val tv = findViewById(R.id.questionfield) as TextView
tv.text = "What is your name?"
}
}