Урок 24: Теорема Пифагора

MainActivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.neco_desarrollo.pifagorapp.databinding.ActivityMainBinding
import kotlin.math.pow
import kotlin.math.sqrt

class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}

fun onClickResult(view: View){
if(!isFieldEmpty()){
val result = getString(R.string.result_info) + getResult()
binding.tvResult.text = result
}
}

private fun isFieldEmpty(): Boolean{
binding.apply {
if(edA.text.isNullOrEmpty()) edA.error = "Поле должно быть заполнено"
if(edB.text.isNullOrEmpty()) edB.error = "Поле должно быть заполнено"
return edA.text.isNullOrEmpty() || edB.text.isNullOrEmpty()
}
}

private fun getResult(): String{
val a: Double
val b: Double
binding.apply {
a = edA.text.toString().toDouble()
b = edB.text.toString().toDouble()
}
return sqrt((a.pow(2) + b.pow(2))).toString()
}
}

strings.xml

<resources>
<string name="app_name">PifagorApp</string>
<string name="title">Вычислить "С"</string>
<string name="result_info">Результат:&#160;</string>
<string name="result">Результат</string>
<string name="input_a">Введите значение \"a\"</string>
<string name="input_b">Введите значение \"b\"</string>
</resources>

activity_main.xml

<?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">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/title"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@drawable/image" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />

<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/result_info"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/guideline2" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="1dp"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/tvResult">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/input_a"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayoutb"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="1dp"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/input_b"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:onClick="onClickResult"
android:text="@string/result"
app:backgroundTint="@color/grey"
app:layout_constraintEnd_toEndOf="@+id/textInputLayoutb"
app:layout_constraintStart_toStartOf="@+id/textInputLayoutb"
app:layout_constraintTop_toBottomOf="@+id/textInputLayoutb" />

</androidx.constraintlayout.widget.ConstraintLayout>

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *