https://pastein.ru/t/mt6

  скопируйте уникальную ссылку для отправки

Загрузка данных


package com.ngpytdev.lection

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import android.graphics.Color
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import kotlin.random.Random
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.widget.*

class MainActivity : AppCompatActivity() {
    private var counter = 0
    private var elementCount = 3
    private var alphaIncreasing = true

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

        // Toast
        findViewById<Button>(R.id.btnToast).setOnClickListener {
            Toast.makeText(this, "Привет!", Toast.LENGTH_SHORT).show()
        }

        // Смена текста
        val btnChange = findViewById<Button>(R.id.btnChangeText)
        btnChange.setOnClickListener {
            btnChange.text = "Нажато!"
        }

        // Счётчик
        val tvCounter = findViewById<TextView>(R.id.tvCounter)
        findViewById<Button>(R.id.btnCounter).setOnClickListener {
            counter++
            tvCounter.text = counter.toString()
        }

        // Скрыть/Показать TextView
        val tvToggle = findViewById<TextView>(R.id.tvHideShow)
        findViewById<Button>(R.id.btnToggleText).setOnClickListener {
            tvToggle.visibility = if (tvToggle.visibility == View.VISIBLE) View.GONE else View.VISIBLE
        }

        // Копирование текста
        val etInput = findViewById<EditText>(R.id.etInput)
        val tvCopied = findViewById<TextView>(R.id.tvCopiedText)
        findViewById<Button>(R.id.btnCopyText).setOnClickListener {
            tvCopied.text = etInput.text.toString()
        }

        // Проверка пустого поля
        findViewById<Button>(R.id.btnCheckEmpty).setOnClickListener {
            val text = etInput.text.toString()
            Toast.makeText(this, if (text.isEmpty()) "Поле пустое!" else "Текст: $text", Toast.LENGTH_SHORT).show()
        }

        // Сумма двух чисел
        findViewById<Button>(R.id.btnSum).setOnClickListener {
            val num1 = findViewById<EditText>(R.id.etNum1).text.toString().toIntOrNull() ?: 0
            val num2 = findViewById<EditText>(R.id.etNum2).text.toString().toIntOrNull() ?: 0
            findViewById<TextView>(R.id.tvSumResult).text = "Сумма: ${num1 + num2}"
        }

        // Очистка поля
        findViewById<Button>(R.id.btnClearText).setOnClickListener {
            etInput.setText("")
        }

        // Цвет текста
        val tvColorSize = findViewById<TextView>(R.id.tvColorSize)
        findViewById<Button>(R.id.btnChangeColor).setOnClickListener {
            val rnd = Random.Default
            tvColorSize.setTextColor(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)))
        }

        // Размер текста
        findViewById<Button>(R.id.btnIncreaseSize).setOnClickListener {
            tvColorSize.textSize += 2
        }

        // Открытие Activity
        findViewById<Button>(R.id.btnOpenActivity).setOnClickListener {
            val text = findViewById<EditText>(R.id.etToSend).text.toString()
            val intent = Intent(this, SecondActivity::class.java)
            intent.putExtra("message", text)
            startActivity(intent)
        }

        // CheckBox
        val checkBox = findViewById<CheckBox>(R.id.checkBox)
        findViewById<Button>(R.id.btnCheckBox).setOnClickListener {
            val msg = if (checkBox.isChecked) "Галочка поставлена" else "Галочка не поставлена"
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
        }

        // RadioGroup
        findViewById<RadioGroup>(R.id.radioGroup).setOnCheckedChangeListener { _, checkedId ->
            val color = when (checkedId) {
                R.id.rbRed -> Color.RED
                R.id.rbGreen -> Color.GREEN
                R.id.rbBlue -> Color.BLUE
                else -> Color.WHITE
            }
            findViewById<LinearLayout>(R.id.mainLayout)?.setBackgroundColor(color)
        }

        // Switch
        val layout = findViewById<LinearLayout>(R.id.mainLayout)
        findViewById<Switch>(R.id.switchNightMode).setOnCheckedChangeListener { _, isChecked ->
            layout?.setBackgroundColor(if (isChecked) Color.BLACK else Color.WHITE)
        }

        // ListView
        val listView = findViewById<ListView>(R.id.listView)
        val listItems = mutableListOf("Элемент 1", "Элемент 2", "Элемент 3")
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems)
        listView.adapter = adapter
        findViewById<Button>(R.id.btnAddElement).setOnClickListener {
            elementCount++
            listItems.add("Элемент $elementCount")
            adapter.notifyDataSetChanged()
        }

        // SeekBar
        val tvVolume = findViewById<TextView>(R.id.tvVolume)
        findViewById<SeekBar>(R.id.seekBar).setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                tvVolume.text = "Громкость: $progress"
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {}
            override fun onStopTrackingTouch(seekBar: SeekBar?) {}
        })

        // ProgressBar
        val progressBar = findViewById<ProgressBar>(R.id.progressBar)
        findViewById<Button>(R.id.btnStartProgress).setOnClickListener {
            progressBar.visibility = View.VISIBLE
            Handler(Looper.getMainLooper()).postDelayed({
                progressBar.visibility = View.GONE
            }, 3000)
        }

        // ImageView fade
        val imageView = findViewById<ImageView>(R.id.imageView)
        findViewById<Button>(R.id.btnFadeImage).setOnClickListener {
            val newAlpha = if (alphaIncreasing) 1f else 0f
            imageView.animate().alpha(newAlpha).setDuration(1000).start()
            alphaIncreasing = !alphaIncreasing
        }
    }
}