Advertisement
deyanivanov966

Упражнение 3. Използване на контроли

Jun 2nd, 2022
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.92 KB | None | 0 0
  1. <TextView
  2.     android:id="@+id/lbl"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content"
  5.     android:text="Hello World!"
  6.     app:layout_constraintBottom_toBottomOf="parent"
  7.     app:layout_constraintLeft_toLeftOf="parent"
  8.     app:layout_constraintRight_toRightOf="parent"
  9.     app:layout_constraintTop_toTopOf="parent" />
  10.  
  11. <ImageView
  12.     android:id="@+id/imageView"
  13.     android:layout_width="wrap_content"
  14.     android:layout_height="wrap_content"
  15.     android:layout_marginStart="34dp"
  16.     android:layout_marginTop="16dp"
  17.     android:onClick="btn_Pict"
  18.     android:tag="0"
  19.     app:layout_constraintStart_toStartOf="parent"
  20.     app:layout_constraintTop_toTopOf="parent"
  21.     app:srcCompat="@drawable/sv1" />
  22.  
  23. <Button
  24.     android:id="@+id/button"
  25.     android:layout_width="wrap_content"
  26.     android:layout_height="wrap_content"
  27.     android:layout_marginTop="33dp"
  28.     android:layout_marginEnd="84dp"
  29.     android:onClick="btn_Pict"
  30.     android:text="Button"
  31.     app:layout_constraintEnd_toEndOf="parent"
  32.     app:layout_constraintTop_toTopOf="parent" />
  33.  
  34. <Switch
  35.     android:id="@+id/switch1"
  36.     android:layout_width="wrap_content"
  37.     android:layout_height="wrap_content"
  38.     android:layout_marginTop="156dp"
  39.     android:layout_marginEnd="72dp"
  40.     android:text="Switch"
  41.     app:layout_constraintEnd_toEndOf="parent"
  42.     app:layout_constraintTop_toTopOf="parent" />
  43.  
  44. <RatingBar
  45.     android:id="@+id/ratingBar"
  46.     android:layout_width="wrap_content"
  47.     android:layout_height="wrap_content"
  48.     android:layout_marginTop="52dp"
  49.     android:layout_marginEnd="21dp"
  50.     android:numStars="4"
  51.     app:layout_constraintEnd_toEndOf="@+id/switch1"
  52.     app:layout_constraintTop_toBottomOf="@+id/switch1" />
  53.  
  54.  
  55. import android.graphics.Color
  56. import androidx.appcompat.app.AppCompatActivity
  57. import android.os.Bundle
  58. import android.view.View
  59. import android.webkit.WebView
  60. import android.widget.*
  61. import androidx.core.view.isVisible
  62.  
  63. class MainActivity : AppCompatActivity() {
  64.     override fun onCreate(savedInstanceState: Bundle?) {
  65.         super.onCreate(savedInstanceState)
  66.         setContentView(R.layout.activity_main)
  67.  
  68.         findViewById<Switch>(R.id.switch1).setOnClickListener {
  69.             val sw=findViewById<Switch>(R.id.switch1)
  70.             val iv=findViewById<ImageView>(R.id.imageView)
  71.             iv.isVisible=sw.isChecked
  72.             if( sw.isChecked )
  73.                 Toast.makeText(this, "ON", Toast.LENGTH_SHORT).show()
  74.             else
  75.                 Toast.makeText(this, "OFF", Toast.LENGTH_SHORT).show()
  76.         }
  77.         findViewById<RatingBar>(R.id.ratingBar).setOnRatingBarChangeListener { ratingBar, fl, b ->
  78.             //val rb=findViewById<RatingBar>(R.id.ratingBar)
  79.             val tv=findViewById<TextView>(R.id.lbl)
  80.             //val st=rb.rating
  81.             //tv.text="Стойност: "+st.toString()
  82.             //tv.text="Стойност: "+fl.toString()
  83.             //1 Вариант
  84.             /*if(fl<=1) tv.text="Една звезда"
  85.             else if(fl<=2) tv.text="Две звезда"
  86.             else if(fl<=3) tv.text="Три звезда"
  87.             else tv.text="Четири звезда"*/
  88.             //2 Вариант
  89.             /*when((fl-0.5).toInt()) {
  90.                 0-> tv.text="Една звезда"
  91.                 1->tv.text="Две звезда"
  92.                 2->tv.text="Три звезда"
  93.                 3->tv.text="Четири звезда"
  94.             }*/
  95.             //3 Вариант
  96.             val imena= listOf("Една звезда","Две звезда","Три звезда","Четири звезда")
  97.             tv.text=imena[(fl-0.5).toInt()]
  98.             if( fl!=fl.toInt().toFloat()) tv.text=tv.text.toString()+" и половина"
  99.         }
  100.  
  101.     }
  102.  
  103.     fun btn_Pict(view: View)
  104.     {
  105.         val tv=findViewById<TextView>(R.id.lbl)
  106.         val iv=findViewById<ImageView>(R.id.imageView)
  107.         iv.tag=iv.tag.toString().toInt()+1
  108.         if(iv.tag==3 ) iv.tag=0
  109.  
  110.         when(iv.tag) {
  111.             0-> {
  112.                 iv.setImageResource(R.drawable.sv1)
  113.                 tv.text="Зелено"
  114.                 tv.setBackgroundColor(Color.GREEN)
  115.             }
  116.             1->{
  117.                 iv.setImageResource(R.drawable.sv2)
  118.                 tv.text="Жълто"
  119.                 tv.setBackgroundColor(Color.YELLOW)
  120.             }
  121.             2->{
  122.                 iv.setImageResource(R.drawable.sv3)
  123.                 tv.text="Червено"
  124.                 tv.setBackgroundColor(Color.RED)
  125.             }
  126.         }
  127.         /*if( iv.tag==0 ) {
  128.             iv.setImageResource(R.drawable.sv1)
  129.             tv.text="Зелено"
  130.         }
  131.         if( iv.tag==1 ) {
  132.             iv.setImageResource(R.drawable.sv2)
  133.             tv.text="Жълто"
  134.         }
  135.         if( iv.tag==2 ) {
  136.             iv.setImageResource(R.drawable.sv3)
  137.             tv.text="Червено"
  138.         }*/
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement