Advertisement
deyanivanov966

Упражнение 2. Работа със стрингове

Jun 2nd, 2022
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 8.95 KB | None | 0 0
  1. package com.example.upr2_strings
  2.  
  3. import android.graphics.Color
  4. import androidx.appcompat.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.text.TextUtils
  7. import android.view.View
  8. import android.widget.CheckBox
  9. import android.widget.EditText
  10. import android.widget.TextView
  11. import android.widget.Toast
  12.  
  13. class MainActivity : AppCompatActivity() {
  14.     override fun onCreate(savedInstanceState: Bundle?) {
  15.         super.onCreate(savedInstanceState)
  16.         setContentView(R.layout.activity_main)
  17.         //test_string()
  18.     }
  19.  
  20.     fun Proverka_Ime():Boolean
  21.     {
  22.         val ime=findViewById<EditText>(R.id.editTextTextPersonName).text.toString().uppercase()
  23.  
  24.         if( ime.isEmpty() ) {
  25.             Toast.makeText(this, "Въведете имена", Toast.LENGTH_SHORT).show()
  26.             return false
  27.         }
  28.         if( ime.matches("[A-Z -]+".toRegex())==false )
  29.         {
  30.             Toast.makeText(this, "Използвайте само букви", Toast.LENGTH_SHORT).show()
  31.             return false
  32.         }
  33.         //val sp1=ime.indexOf(' ')
  34.         //val sp2=ime.indexOf(' ', sp1+1)
  35.         val imena=ime.split(' ')
  36.         //if( sp2==-1) {
  37.         if( imena.size!=3 ) {
  38.             Toast.makeText(this, "Въведете три имена", Toast.LENGTH_SHORT).show()
  39.             return false
  40.         } else {
  41.             val tv2=findViewById<TextView>(R.id.imena)
  42.             tv2.text="Име:"+imena[0]+" Презиме:"+imena[1]+" Фамилия:"+imena[2]
  43.         }
  44.  
  45.         return true
  46.     }
  47.  
  48.     fun Proverka_Nomer():Boolean
  49.     {
  50.         val nomer=findViewById<EditText>(R.id.editTextPhone).text.toString()
  51.         if( nomer.length==0 ) {
  52.             Toast.makeText(this, "Липсва въведен номер", Toast.LENGTH_SHORT).show()
  53.             return false
  54.         }
  55.         if( nomer.matches("[0-9]+".toRegex())==false) {
  56.             Toast.makeText(this, "Използвайте само цифри", Toast.LENGTH_SHORT).show()
  57.             return false
  58.         }
  59.         if( nomer.length!=10 || nomer.startsWith("08")==false) {
  60.                 Toast.makeText(this, "Непълен номер", Toast.LENGTH_SHORT).show()
  61.                 return false
  62.         }
  63.         return true
  64.     }
  65.  
  66.     fun String.isEmailValid(): Boolean {
  67.         return !TextUtils.isEmpty(this) && android.util.Patterns.EMAIL_ADDRESS.matcher(this).matches()
  68.     }
  69.  
  70.     fun Proverka_Email():Boolean
  71.     {
  72.         val email=findViewById<EditText>(R.id.editTextTextEmailAddress).text.toString()
  73.  
  74.         /*if( email=="" ) {
  75.             Toast.makeText(this, "Липсва въведен имейл", Toast.LENGTH_SHORT).show()
  76.             return false
  77.         }
  78.         if( email.indexOfAny(charArrayOf('!','#','$','%','?',' ','=','*'))>=0 ){
  79.             Toast.makeText(this, "Некоректни символи", Toast.LENGTH_SHORT).show()
  80.             return false
  81.         }
  82.         val at=email.indexOf('@')
  83.         val at2=email.indexOf('@',at+1)
  84.         val dot=email.indexOf('.',at+1)
  85.         val lastdot=email.lastIndexOf('.')
  86.         val many=email.indexOf("..")
  87.         if( at==-1 || dot==-1 || at2>0 || lastdot==email.length-1 || many>=0) {
  88.             Toast.makeText(this, "Некоректен имейл", Toast.LENGTH_SHORT).show()
  89.             return false
  90.         }
  91.  
  92.         return true*/
  93.         if( email.isEmailValid() ) return true
  94.         else {
  95.             Toast.makeText(this, "Некоректен имейл", Toast.LENGTH_SHORT).show()
  96.             return false
  97.         }
  98.     }
  99.  
  100.     fun btn_check(view:View)
  101.     {
  102.         val chk1=findViewById<CheckBox>(R.id.checkBox)
  103.         val chk2=findViewById<CheckBox>(R.id.checkBox2)
  104.         val chk3=findViewById<CheckBox>(R.id.checkBox3)
  105.         val tv=findViewById<TextView>(R.id.lbl)
  106.         chk1.isChecked=Proverka_Ime()
  107.         chk2.isChecked=Proverka_Nomer()
  108.         chk3.isChecked=Proverka_Email()
  109.         if( chk1.isChecked && chk2.isChecked && chk3.isChecked) {
  110.             tv.text="OK"
  111.             tv.setBackgroundColor(Color.GREEN)
  112.         } else
  113.         {
  114.             tv.text="Има непопълнени полета"
  115.             tv.setBackgroundColor(Color.RED)
  116.         }
  117.  
  118.     }
  119.  
  120.     fun test_string()
  121.     {
  122.         val tv=findViewById<TextView>(R.id.lbl)
  123.         val tmp="Здравейте"
  124.         var rez=""
  125.         // 1
  126.         /*tmp.forEach {
  127.             rez=rez+it+" "
  128.         }*/
  129.         // 2            tmp.length->9
  130.         /*for( a in 0..tmp.length-1)
  131.             //rez=rez+a.toString()+" "
  132.             rez=rez+tmp[a]+" "*/
  133.         // 3
  134.         for(ch in tmp)
  135.             rez=rez+ch+" "
  136.  
  137.         tv.text=rez
  138.     }
  139. }
  140.  
  141. <?xml version="1.0" encoding="utf-8"?>
  142. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  143.     xmlns:app="http://schemas.android.com/apk/res-auto"
  144.     xmlns:tools="http://schemas.android.com/tools"
  145.     android:layout_width="match_parent"
  146.     android:layout_height="match_parent"
  147.     tools:context=".MainActivity">
  148.  
  149.     <EditText
  150.         android:id="@+id/editTextTextPersonName"
  151.         android:layout_width="wrap_content"
  152.         android:layout_height="wrap_content"
  153.         android:layout_marginStart="30dp"
  154.         android:ems="10"
  155.         android:hint="Три имена"
  156.         android:inputType="textPersonName"
  157.         android:text="Ivan Petrov Minev"
  158.         app:layout_constraintBaseline_toBaselineOf="@+id/checkBox"
  159.         app:layout_constraintStart_toStartOf="parent" />
  160.  
  161.     <EditText
  162.         android:id="@+id/editTextPhone"
  163.         android:layout_width="wrap_content"
  164.         android:layout_height="wrap_content"
  165.         android:layout_marginStart="27dp"
  166.         android:layout_marginTop="114dp"
  167.         android:ems="10"
  168.         android:hint="Телефон"
  169.         android:inputType="phone"
  170.         android:text="0884561234"
  171.         app:layout_constraintStart_toStartOf="parent"
  172.         app:layout_constraintTop_toTopOf="parent" />
  173.  
  174.     <EditText
  175.         android:id="@+id/editTextTextEmailAddress"
  176.         android:layout_width="wrap_content"
  177.         android:layout_height="wrap_content"
  178.         android:layout_marginTop="25dp"
  179.         android:ems="10"
  180.         android:hint="Имейл"
  181.         android:inputType="textEmailAddress"
  182.         app:layout_constraintEnd_toStartOf="@+id/button"
  183.         app:layout_constraintStart_toStartOf="@+id/button"
  184.         app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />
  185.  
  186.     <Button
  187.         android:id="@+id/button"
  188.         android:layout_width="wrap_content"
  189.         android:layout_height="wrap_content"
  190.         android:layout_marginTop="27dp"
  191.         android:layout_marginEnd="23dp"
  192.         android:onClick="btn_check"
  193.         android:text="Провери"
  194.         app:layout_constraintEnd_toStartOf="@+id/checkBox3"
  195.         app:layout_constraintTop_toBottomOf="@+id/editTextTextEmailAddress" />
  196.  
  197.     <CheckBox
  198.         android:id="@+id/checkBox"
  199.         android:layout_width="wrap_content"
  200.         android:layout_height="wrap_content"
  201.         android:layout_marginStart="22dp"
  202.         android:layout_marginBottom="16dp"
  203.         android:editable="false"
  204.         android:enabled="false"
  205.         app:layout_constraintBottom_toTopOf="@+id/editTextPhone"
  206.         app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName" />
  207.  
  208.     <CheckBox
  209.         android:id="@+id/checkBox2"
  210.         android:layout_width="wrap_content"
  211.         android:layout_height="wrap_content"
  212.         android:layout_marginTop="13dp"
  213.         android:editable="false"
  214.         android:enabled="false"
  215.         app:layout_constraintStart_toStartOf="@+id/checkBox"
  216.         app:layout_constraintTop_toBottomOf="@+id/checkBox" />
  217.  
  218.     <CheckBox
  219.         android:id="@+id/checkBox3"
  220.         android:layout_width="wrap_content"
  221.         android:layout_height="wrap_content"
  222.         android:editable="false"
  223.         android:enabled="false"
  224.         app:layout_constraintBaseline_toBaselineOf="@+id/editTextTextEmailAddress"
  225.         app:layout_constraintStart_toStartOf="@+id/checkBox2" />
  226.  
  227.     <TextView
  228.         android:id="@+id/lbl"
  229.         android:layout_width="wrap_content"
  230.         android:layout_height="wrap_content"
  231.         android:text="Hello World!"
  232.         app:layout_constraintBottom_toBottomOf="parent"
  233.         app:layout_constraintLeft_toLeftOf="parent"
  234.         app:layout_constraintRight_toRightOf="parent"
  235.         app:layout_constraintTop_toTopOf="parent"
  236.         app:layout_constraintVertical_bias="0.595" />
  237.  
  238.     <TextView
  239.         android:id="@+id/imena"
  240.         android:layout_width="wrap_content"
  241.         android:layout_height="wrap_content"
  242.         android:text="TextView"
  243.         app:layout_constraintBottom_toBottomOf="parent"
  244.         app:layout_constraintEnd_toEndOf="parent"
  245.         app:layout_constraintStart_toStartOf="parent"
  246.         app:layout_constraintTop_toTopOf="parent"
  247.         app:layout_constraintVertical_bias="0.702" />
  248.  
  249. </androidx.constraintlayout.widget.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement