изменить значение textview
Не могу найти рабочий способ(или не могу найти правильное применение этих способок. Пробовал и различные databinding и через TextView textView = findViewById(R.id.textView)
И все равно не могу изменить текст в TextView.
MainActivity.kt
package com.example.myfirstapp1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.databinding.DataBindingUtil
//import com.example.myfirstapp1.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//val binding: ActivityMainBinding =
//DataBindingUtil.setContentView(this, R.layout.activity_main)
//TextView textView = findViewById(R.id.textView)
}
fun toastMe(view: View) {
val myToast = Toast.makeText(this, "Toast", Toast.LENGTH_SHORT)
myToast.show()
}
fun countMe(view: View) {
TextView textView = findViewById(R.id.textView)
//
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="@color/screenBackground"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="151dp"
android:layout_height="40dp"
android:fontFamily="sans-serif-smallcaps"
android:text="@{helw}"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.1" />
<Button
android:onClick="toastMe"
android:id="@+id/toast_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:background="@color/bottunBackground"
android:text="toast"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.501" />
<Button
android:id="@+id/count_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/bottunBackground"
android:text="count"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/random_button"
app:layout_constraintStart_toEndOf="@+id/toast_button"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/random_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:background="@color/bottunBackground"
android:text="random"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.501" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="119dp"
tools:layout_editor_absoluteY="193dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Источник: Stack Overflow на русском