deyanivanov966

Exercise 5 11.03.2022 - Binding КОД ПРЕПОДАВАТЕЛ

Mar 14th, 2022 (edited)
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.05 KB | None | 0 0
  1. import android.graphics.Color
  2. import androidx.appcompat.app.AppCompatActivity
  3. import android.os.Bundle
  4. import android.view.View
  5. import androidx.databinding.DataBindingUtil
  6. import com.example.upr5_binding.databinding.ActivityMainBinding
  7.  
  8. class MainActivity : AppCompatActivity() {
  9.     private lateinit var binding: ActivityMainBinding
  10.  
  11.     override fun onCreate(savedInstanceState: Bundle?) {
  12.         super.onCreate(savedInstanceState)
  13.         //setContentView(R.layout.activity_main)
  14.         //binding=DataBindingUtil.setContentView(this, R.layout.activity_main)
  15.         binding = ActivityMainBinding.inflate(layoutInflater)
  16.         setContentView(binding.root)
  17.         //binding.textView.text="Ok"
  18.     }
  19.  
  20.     fun chetno(view: View)
  21.     {
  22.         val chislo=binding.editTextNumber.text.toString().toInt()
  23.         if( chislo%2==0) {
  24.             binding.textView.text="Четно"
  25.             binding.textView.setBackgroundColor(Color.GREEN)
  26.         } else {
  27.             binding.textView.text="Нечетно"
  28.             binding.textView.setBackgroundColor(Color.YELLOW)
  29.         }
  30.     }
  31.  
  32.     fun btn_oper(view: View) {
  33.         var tel=binding.editTextPhone.text.toString()
  34.         var nomer="None"
  35.         if( tel.startsWith("359")) tel=tel.replace("359","0")
  36.         // 35988 -> 088
  37.         if( tel.startsWith("088") ) {
  38.             binding.imageView.setImageResource(R.drawable.a1)
  39.             //nomer="A1-"+tel.substring(3)
  40.             nomer=tel.replace("088","A1-")
  41.         } else
  42.         if( tel.startsWith("087")) {
  43.             binding.imageView.setImageResource(R.drawable.vivacom)
  44.             //nomer="Vivacom-"+tel.substring(3)
  45.             nomer=tel.replace("087","Vivacom-")
  46.         } else
  47.         if( tel.startsWith("089")) {
  48.             binding.imageView.setImageResource(R.drawable.yettel)
  49.             //nomer="Yettel-"+tel.substring(3)
  50.             nomer=tel.replace("089","Yettel-")
  51.         } else
  52.             binding.imageView.setImageResource(R.drawable.vapros)
  53.         binding.textView.text=nomer
  54.     }
  55.     // Въведено число в EditText, в TextView да се изведат броят на цифрите с думи
  56.     // 34 -> двуцифрено,    123 -> трицифрено ...
  57.     fun btn_zad(view: View) {
  58.         //1
  59.         /*val ch=binding.editTextNumber.text.toString()
  60.         if( ch.length==1 )  binding.textView.text="Едноцифрено"
  61.         if( ch.length==2 )  binding.textView.text="Двуоцифрено"
  62.         if( ch.length==3 )  binding.textView.text="Трицифрено"
  63.         if( ch.length>3 )  binding.textView.text="Многоцифрено"*/
  64.         //2
  65.         /*val ch=binding.editTextNumber.text.toString()
  66.         when(ch.length) {
  67.             1 ->binding.textView.text="Едноцифрено"
  68.             2->binding.textView.text="Двуоцифрено"
  69.             3->binding.textView.text="Трицифрено"
  70.             else -> binding.textView.text="Многоцифрено"
  71.         }*/
  72.         //3
  73.         val ch=binding.editTextNumber.text.toString()
  74.         val vid= listOf("Многоцифрено","Едноцифрено","Двуоцифрено","Трицифрено")
  75.         if(ch.length>3 ) binding.textView.text=vid[0]
  76.         else binding.textView.text=vid[ch.length]
  77.     }
  78. }
  79. <androidx.constraintlayout.widget.ConstraintLayout
  80.     android:layout_width="match_parent"
  81.     android:layout_height="match_parent"
  82.     tools:context=".MainActivity">
  83.  
  84.     <TextView
  85.         android:id="@+id/textView"
  86.         android:layout_width="wrap_content"
  87.         android:layout_height="wrap_content"
  88.         android:text="Hello World!"
  89.         app:layout_constraintBottom_toBottomOf="parent"
  90.         app:layout_constraintLeft_toLeftOf="parent"
  91.         app:layout_constraintRight_toRightOf="parent"
  92.         app:layout_constraintTop_toTopOf="parent" />
  93.  
  94.     <EditText
  95.         android:id="@+id/editTextNumber"
  96.         android:layout_width="wrap_content"
  97.         android:layout_height="wrap_content"
  98.         android:layout_marginStart="37dp"
  99.         android:layout_marginTop="27dp"
  100.         android:layout_marginBottom="352dp"
  101.         android:ems="10"
  102.         android:inputType="number"
  103.         app:layout_constraintBottom_toTopOf="@+id/imageView"
  104.         app:layout_constraintStart_toStartOf="parent"
  105.         app:layout_constraintTop_toTopOf="parent" />
  106.  
  107.     <Button
  108.         android:id="@+id/button"
  109.         android:layout_width="wrap_content"
  110.         android:layout_height="wrap_content"
  111.         android:layout_marginStart="45dp"
  112.         android:layout_marginTop="32dp"
  113.         android:onClick="btn_zad"
  114.         android:text="Button"
  115.         app:layout_constraintStart_toStartOf="parent"
  116.         app:layout_constraintTop_toBottomOf="@+id/editTextNumber" />
  117.  
  118.     <EditText
  119.         android:id="@+id/editTextPhone"
  120.         android:layout_width="wrap_content"
  121.         android:layout_height="wrap_content"
  122.         android:layout_marginTop="17dp"
  123.         android:ems="10"
  124.         android:inputType="phone"
  125.         app:layout_constraintEnd_toEndOf="@+id/button2"
  126.         app:layout_constraintStart_toEndOf="@+id/button2"
  127.         app:layout_constraintTop_toBottomOf="@+id/button" />
  128.  
  129.     <Button
  130.         android:id="@+id/button2"
  131.         android:layout_width="wrap_content"
  132.         android:layout_height="wrap_content"
  133.         android:layout_marginStart="53dp"
  134.         android:layout_marginTop="28dp"
  135.         android:onClick="btn_oper"
  136.         android:text="Operator"
  137.         app:layout_constraintStart_toStartOf="parent"
  138.         app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />
  139.  
  140.     <ImageView
  141.         android:id="@+id/imageView"
  142.         android:layout_width="269dp"
  143.         android:layout_height="0dp"
  144.         android:layout_marginStart="53dp"
  145.         android:layout_marginBottom="118dp"
  146.         app:layout_constraintBottom_toBottomOf="parent"
  147.         app:layout_constraintStart_toStartOf="parent"
  148.         app:layout_constraintTop_toBottomOf="@+id/editTextNumber"
  149.         app:srcCompat="@drawable/vapros" />
  150.  
  151. </androidx.constraintlayout.widget.ConstraintLayout>
Add Comment
Please, Sign In to add comment