Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <TextView
- android:id="@+id/lbl"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello World!"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="34dp"
- android:layout_marginTop="16dp"
- android:onClick="btn_Pict"
- android:tag="0"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:srcCompat="@drawable/sv1" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="33dp"
- android:layout_marginEnd="84dp"
- android:onClick="btn_Pict"
- android:text="Button"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Switch
- android:id="@+id/switch1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="156dp"
- android:layout_marginEnd="72dp"
- android:text="Switch"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <RatingBar
- android:id="@+id/ratingBar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="52dp"
- android:layout_marginEnd="21dp"
- android:numStars="4"
- app:layout_constraintEnd_toEndOf="@+id/switch1"
- app:layout_constraintTop_toBottomOf="@+id/switch1" />
- import android.graphics.Color
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.view.View
- import android.webkit.WebView
- import android.widget.*
- import androidx.core.view.isVisible
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- findViewById<Switch>(R.id.switch1).setOnClickListener {
- val sw=findViewById<Switch>(R.id.switch1)
- val iv=findViewById<ImageView>(R.id.imageView)
- iv.isVisible=sw.isChecked
- if( sw.isChecked )
- Toast.makeText(this, "ON", Toast.LENGTH_SHORT).show()
- else
- Toast.makeText(this, "OFF", Toast.LENGTH_SHORT).show()
- }
- findViewById<RatingBar>(R.id.ratingBar).setOnRatingBarChangeListener { ratingBar, fl, b ->
- //val rb=findViewById<RatingBar>(R.id.ratingBar)
- val tv=findViewById<TextView>(R.id.lbl)
- //val st=rb.rating
- //tv.text="Стойност: "+st.toString()
- //tv.text="Стойност: "+fl.toString()
- //1 Вариант
- /*if(fl<=1) tv.text="Една звезда"
- else if(fl<=2) tv.text="Две звезда"
- else if(fl<=3) tv.text="Три звезда"
- else tv.text="Четири звезда"*/
- //2 Вариант
- /*when((fl-0.5).toInt()) {
- 0-> tv.text="Една звезда"
- 1->tv.text="Две звезда"
- 2->tv.text="Три звезда"
- 3->tv.text="Четири звезда"
- }*/
- //3 Вариант
- val imena= listOf("Една звезда","Две звезда","Три звезда","Четири звезда")
- tv.text=imena[(fl-0.5).toInt()]
- if( fl!=fl.toInt().toFloat()) tv.text=tv.text.toString()+" и половина"
- }
- }
- fun btn_Pict(view: View)
- {
- val tv=findViewById<TextView>(R.id.lbl)
- val iv=findViewById<ImageView>(R.id.imageView)
- iv.tag=iv.tag.toString().toInt()+1
- if(iv.tag==3 ) iv.tag=0
- when(iv.tag) {
- 0-> {
- iv.setImageResource(R.drawable.sv1)
- tv.text="Зелено"
- tv.setBackgroundColor(Color.GREEN)
- }
- 1->{
- iv.setImageResource(R.drawable.sv2)
- tv.text="Жълто"
- tv.setBackgroundColor(Color.YELLOW)
- }
- 2->{
- iv.setImageResource(R.drawable.sv3)
- tv.text="Червено"
- tv.setBackgroundColor(Color.RED)
- }
- }
- /*if( iv.tag==0 ) {
- iv.setImageResource(R.drawable.sv1)
- tv.text="Зелено"
- }
- if( iv.tag==1 ) {
- iv.setImageResource(R.drawable.sv2)
- tv.text="Жълто"
- }
- if( iv.tag==2 ) {
- iv.setImageResource(R.drawable.sv3)
- tv.text="Червено"
- }*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement