Advertisement
simeonnikolov

Untitled

Feb 26th, 2025 (edited)
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.18 KB | None | 0 0
  1. package com.example.home1
  2.  
  3. import android.graphics.Color
  4. import android.os.Bundle
  5. import android.view.View
  6. import android.widget.Button
  7. import android.widget.EditText
  8. import android.widget.TextView
  9. import android.widget.Toast
  10. import androidx.activity.enableEdgeToEdge
  11. import androidx.appcompat.app.AppCompatActivity
  12. import androidx.core.view.ViewCompat
  13. import androidx.core.view.WindowInsetsCompat
  14. import org.w3c.dom.Text
  15.  
  16. class MainActivity : AppCompatActivity() {
  17.     override fun onCreate(savedInstanceState: Bundle?) {
  18.         super.onCreate(savedInstanceState)
  19.         enableEdgeToEdge()
  20.         setContentView(R.layout.activity_main)
  21.         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
  22.             val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
  23.             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
  24.             insets
  25.         }
  26.  
  27.         val mybtn = findViewById<Button>(R.id.button)
  28.         mybtn.setOnClickListener {
  29.             val year = findViewById<EditText>(R.id.editTextNumber)
  30.             val tv = findViewById<TextView>(R.id.textView)
  31.             val st = year.text.toString().toInt()
  32.             if (st < 1950 || st > 2020) {
  33.                 Toast.makeText(this, "Грешна година", Toast.LENGTH_SHORT).show()
  34.             } else {
  35.                 val age = 2025-st
  36.                 tv.text = "Ти си на $age години"
  37.  
  38.                 val tv = findViewById<TextView>(R.id.textView2)
  39.  
  40.                 if (age < 13) {
  41.                     tv.text = "Дете"
  42.                     tv.setBackgroundColor(Color.GREEN)
  43.                 } else if (age < 20) {
  44.                     tv.text = "Тинейджър"
  45.                     tv.setBackgroundColor(Color.RED)
  46.                 } else if (age < 68) {
  47.                     tv.text = "Възрастен"
  48.                     tv.setBackgroundColor(Color.BLUE)
  49.                 } else {
  50.                     tv.text = "Пенсионер"
  51.                     tv.setBackgroundColor(Color.GRAY)
  52.                 }
  53.             }
  54.         }
  55.     }
  56.  
  57.     fun btn_exit(view: View) {
  58.         finish()
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement