Vinculacion de datos
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="32dp"
tools:context=".MainActivity">
<[Link]
android:id="@+id/lblTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="DIVISION"
android:textStyle="bold"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<[Link]
android:id="@+id/InA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/[Link]"
app:errorEnabled="true"
app:errorTextAppearance="@style/[Link]
.OutlinedBox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/lblTitulo">
<[Link]
android:id="@+id/campoA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="*A"/>
</[Link]>
<[Link]
android:id="@+id/InB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/[Link]"
app:errorEnabled="true"
app:errorTextAppearance="@style/[Link]
.OutlinedBox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/InA">
<[Link]
android:id="@+id/campoB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="*B"/>
</[Link]>
<[Link]
android:id="@+id/btnDividir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-24dp"
android:text="DIVIDIR"
app:layout_constraintBottom_toBottomOf="@+id/InB"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<[Link]
android:id="@+id/txtRESULTADO"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/[Link]"
app:errorEnabled="true"
app:errorTextAppearance="@style/[Link]
.OutlinedBox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnDividir">
<[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:hint="RESULTADO"/>
</[Link]>
</[Link]>
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
private lateinit var inA: TextInputLayout
private lateinit var inB: TextInputLayout
private lateinit var campoA: TextInputEditText
private lateinit var campoB: TextInputEditText
private lateinit var btnDividir: Button
private lateinit var txtREsultado: TextInputLayout
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
inA = findViewById([Link])
inB = findViewById([Link])
campoA = findViewById([Link])
campoB = findViewById([Link])
btnDividir = findViewById([Link])
txtREsultado = findViewById([Link])
[Link] {
calcular()
}
[Link](object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int,
count: Int, after: Int) {
override fun onTextChanged(s: CharSequence?, start: Int,
before: Int, count: Int) {
[Link](null)
override fun afterTextChanged(s: Editable?) {
}
})
[Link](object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int,
count: Int, after: Int) {
override fun onTextChanged(s: CharSequence?, start: Int,
before: Int, count: Int) {
[Link](null)
override fun afterTextChanged(s: Editable?) {
}
})
private fun calcular() {
var ok = true
var a= 0
try {
a = leeAInt(campoA)
}catch (e: NumberFormatException){
ok = false
[Link]("Completa este campo")
}
var b = 0
try {
b = leeBInt(campoB)
}catch (e: NumberFormatException){
ok = false
[Link]("Completa este campo")
}
if (ok){
if(b!=0) {
var cociente = a / b
[Link]?.setText([Link]())
mensaje("Resultado= $cociente")
}else{
[Link]("No se permite la división entre cero")
[Link]?.setText(" ")
mensaje("NO SE PERMITE LA DIVISIÓN ENTRE CERO")
}
}
}
private fun leeAInt(texto: EditText?): Int {
return try {
[Link](null)
texto!!.[Link]().toInt()
}catch (e: NumberFormatException){
throw e
}
private fun leeBInt(texto: EditText?): Int {
return try {
[Link](null)
texto!!.[Link]().toInt()
}catch (e: NumberFormatException){
throw e
}
}
private fun mensaje(mensaje: String) {
[Link](this, mensaje, Toast.LENGTH_LONG).show()
}
}
VINCULACION DE VISTAS
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
binding = [Link](layoutInflater)
setContentView([Link])
[Link] {
calcular()
}
[Link](object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int,
count: Int, after: Int) {
override fun onTextChanged(s: CharSequence?, start: Int,
before: Int, count: Int) {
[Link](null)
override fun afterTextChanged(s: Editable?) {
}
})
[Link](object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int,
count: Int, after: Int) {
override fun onTextChanged(s: CharSequence?, start: Int,
before: Int, count: Int) {
[Link](null)
override fun afterTextChanged(s: Editable?) {
}
})
}
private fun calcular() {
var ok = true
var a= 0
try {
a = leeAInt([Link])
}catch (e: NumberFormatException){
ok = false
[Link]("Completa este campo")
}
var b = 0
try {
b = leeBInt([Link])
}catch (e: NumberFormatException){
ok = false
[Link]("Completa este campo")
}
if (ok){
if(b!=0) {
var cociente = a / b
[Link]?.setText([Link]())
mensaje("Resultado= $cociente")
}else{
[Link]("No se permite la división entre
cero")
[Link]?.setText(" ")
mensaje("NO SE PERMITE LA DIVISIÓN ENTRE CERO")
}
}
}
private fun leeAInt(texto: EditText?): Int {
return try {
[Link](null)
texto!!.[Link]().toInt()
}catch (e: NumberFormatException){
throw e
}
private fun leeBInt(texto: EditText?): Int {
return try {
[Link](null)
texto!!.[Link]().toInt()
}catch (e: NumberFormatException){
throw e
}
}
private fun mensaje(mensaje: String) {
[Link](this, mensaje, Toast.LENGTH_LONG).show()
}
}
plugins {
id '[Link]'
id '[Link]'
}
android {
compileSdk 32
defaultConfig {
applicationId "[Link]"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner
"[Link]"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-
[Link]'), '[Link]'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures{
viewBinding true
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation '[Link]:core-ktx:1.7.0'
implementation '[Link]:appcompat:1.4.1'
implementation '[Link]:material:1.6.0'
implementation '[Link]:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation '[Link]:junit:1.1.3'
androidTestImplementation '[Link]:espresso-
core:3.4.0'
}