Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.upr2_strings
- import android.graphics.Color
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.text.TextUtils
- import android.view.View
- import android.widget.CheckBox
- import android.widget.EditText
- import android.widget.TextView
- import android.widget.Toast
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- //test_string()
- }
- fun Proverka_Ime():Boolean
- {
- val ime=findViewById<EditText>(R.id.editTextTextPersonName).text.toString().uppercase()
- if( ime.isEmpty() ) {
- Toast.makeText(this, "Въведете имена", Toast.LENGTH_SHORT).show()
- return false
- }
- if( ime.matches("[A-Z -]+".toRegex())==false )
- {
- Toast.makeText(this, "Използвайте само букви", Toast.LENGTH_SHORT).show()
- return false
- }
- //val sp1=ime.indexOf(' ')
- //val sp2=ime.indexOf(' ', sp1+1)
- val imena=ime.split(' ')
- //if( sp2==-1) {
- if( imena.size!=3 ) {
- Toast.makeText(this, "Въведете три имена", Toast.LENGTH_SHORT).show()
- return false
- } else {
- val tv2=findViewById<TextView>(R.id.imena)
- tv2.text="Име:"+imena[0]+" Презиме:"+imena[1]+" Фамилия:"+imena[2]
- }
- return true
- }
- fun Proverka_Nomer():Boolean
- {
- val nomer=findViewById<EditText>(R.id.editTextPhone).text.toString()
- if( nomer.length==0 ) {
- Toast.makeText(this, "Липсва въведен номер", Toast.LENGTH_SHORT).show()
- return false
- }
- if( nomer.matches("[0-9]+".toRegex())==false) {
- Toast.makeText(this, "Използвайте само цифри", Toast.LENGTH_SHORT).show()
- return false
- }
- if( nomer.length!=10 || nomer.startsWith("08")==false) {
- Toast.makeText(this, "Непълен номер", Toast.LENGTH_SHORT).show()
- return false
- }
- return true
- }
- fun String.isEmailValid(): Boolean {
- return !TextUtils.isEmpty(this) && android.util.Patterns.EMAIL_ADDRESS.matcher(this).matches()
- }
- fun Proverka_Email():Boolean
- {
- val email=findViewById<EditText>(R.id.editTextTextEmailAddress).text.toString()
- /*if( email=="" ) {
- Toast.makeText(this, "Липсва въведен имейл", Toast.LENGTH_SHORT).show()
- return false
- }
- if( email.indexOfAny(charArrayOf('!','#','$','%','?',' ','=','*'))>=0 ){
- Toast.makeText(this, "Некоректни символи", Toast.LENGTH_SHORT).show()
- return false
- }
- val at=email.indexOf('@')
- val at2=email.indexOf('@',at+1)
- val dot=email.indexOf('.',at+1)
- val lastdot=email.lastIndexOf('.')
- val many=email.indexOf("..")
- if( at==-1 || dot==-1 || at2>0 || lastdot==email.length-1 || many>=0) {
- Toast.makeText(this, "Некоректен имейл", Toast.LENGTH_SHORT).show()
- return false
- }
- return true*/
- if( email.isEmailValid() ) return true
- else {
- Toast.makeText(this, "Некоректен имейл", Toast.LENGTH_SHORT).show()
- return false
- }
- }
- fun btn_check(view:View)
- {
- val chk1=findViewById<CheckBox>(R.id.checkBox)
- val chk2=findViewById<CheckBox>(R.id.checkBox2)
- val chk3=findViewById<CheckBox>(R.id.checkBox3)
- val tv=findViewById<TextView>(R.id.lbl)
- chk1.isChecked=Proverka_Ime()
- chk2.isChecked=Proverka_Nomer()
- chk3.isChecked=Proverka_Email()
- if( chk1.isChecked && chk2.isChecked && chk3.isChecked) {
- tv.text="OK"
- tv.setBackgroundColor(Color.GREEN)
- } else
- {
- tv.text="Има непопълнени полета"
- tv.setBackgroundColor(Color.RED)
- }
- }
- fun test_string()
- {
- val tv=findViewById<TextView>(R.id.lbl)
- val tmp="Здравейте"
- var rez=""
- // 1
- /*tmp.forEach {
- rez=rez+it+" "
- }*/
- // 2 tmp.length->9
- /*for( a in 0..tmp.length-1)
- //rez=rez+a.toString()+" "
- rez=rez+tmp[a]+" "*/
- // 3
- for(ch in tmp)
- rez=rez+ch+" "
- tv.text=rez
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <EditText
- android:id="@+id/editTextTextPersonName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="30dp"
- android:ems="10"
- android:hint="Три имена"
- android:inputType="textPersonName"
- android:text="Ivan Petrov Minev"
- app:layout_constraintBaseline_toBaselineOf="@+id/checkBox"
- app:layout_constraintStart_toStartOf="parent" />
- <EditText
- android:id="@+id/editTextPhone"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="27dp"
- android:layout_marginTop="114dp"
- android:ems="10"
- android:hint="Телефон"
- android:inputType="phone"
- android:text="0884561234"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <EditText
- android:id="@+id/editTextTextEmailAddress"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="25dp"
- android:ems="10"
- android:hint="Имейл"
- android:inputType="textEmailAddress"
- app:layout_constraintEnd_toStartOf="@+id/button"
- app:layout_constraintStart_toStartOf="@+id/button"
- app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="27dp"
- android:layout_marginEnd="23dp"
- android:onClick="btn_check"
- android:text="Провери"
- app:layout_constraintEnd_toStartOf="@+id/checkBox3"
- app:layout_constraintTop_toBottomOf="@+id/editTextTextEmailAddress" />
- <CheckBox
- android:id="@+id/checkBox"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="22dp"
- android:layout_marginBottom="16dp"
- android:editable="false"
- android:enabled="false"
- app:layout_constraintBottom_toTopOf="@+id/editTextPhone"
- app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName" />
- <CheckBox
- android:id="@+id/checkBox2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="13dp"
- android:editable="false"
- android:enabled="false"
- app:layout_constraintStart_toStartOf="@+id/checkBox"
- app:layout_constraintTop_toBottomOf="@+id/checkBox" />
- <CheckBox
- android:id="@+id/checkBox3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:editable="false"
- android:enabled="false"
- app:layout_constraintBaseline_toBaselineOf="@+id/editTextTextEmailAddress"
- app:layout_constraintStart_toStartOf="@+id/checkBox2" />
- <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"
- app:layout_constraintVertical_bias="0.595" />
- <TextView
- android:id="@+id/imena"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.702" />
- </androidx.constraintlayout.widget.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement