package com.example.myapplication
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
// ===== НАШ КОД =====
// 1. Находим элементы
val textView = findViewById<TextView>(R.id.textView)
val editText = findViewById<EditText>(R.id.editTextText)
val imageView = findViewById<ImageView>(R.id.imageView)
// 2. Слушатель ввода текста (работает при каждом нажатии клавиши)
editText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (s.isNullOrEmpty()) {
textView.text = "Hello, friend!"
} else {
textView.text = s
}
}
override fun afterTextChanged(s: Editable?) {}
})
// 3. Клик по картинке — НОВОЕ!
imageView.setOnClickListener {
val name = editText.text.toString()
if (name.isEmpty()) {
textView.text = "Hello, friend!"
} else {
textView.text = "Hello, $name"
}
}
}
}