Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
59 views12 pages

PMDM3

The document describes creating an Android application to sum numbers by clicking a button. It includes the Java code for the main activity class and the XML layout. The main activity gets the values from two edit texts, parses them to doubles, performs the sum, and displays the result in a text view when the button is clicked.

Uploaded by

4203955
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views12 pages

PMDM3

The document describes creating an Android application to sum numbers by clicking a button. It includes the Java code for the main activity class and the XML layout. The main activity gets the values from two edit texts, parses them to doubles, performs the sum, and displays the result in a text view when the button is clicked.

Uploaded by

4203955
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Tarea UT03.1.

Sumar números con botón

MainActivity:
package com.example.calculadora;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

//SE DECLARAN LAS VARIABLES PRIMERO

EditText num1,num2;

Button sumar;

TextView resultado;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//Se les asigna el valor que tienen en el activity main, es decir en el código de diseño

num1=findViewById(R.id.num1);

num2=findViewById(R.id.num2);

sumar=findViewById(R.id.sumar1);

resultado=findViewById(R.id.resultado);

//El setOnClick.. es para asignar la función al boton

sumar.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

try {

double nume1 = Double.parseDouble(num1.getText().toString());

double nume2 = Double.parseDouble(num2.getText().toString());

double resultado1 = nume1 + nume2;

resultado.setText("Resultado: " + resultado1);

} catch (NumberFormatException e) {

resultado.setText("Error: Ingresa números válidos");

});

}}
Activity Main:
<?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">

<androidx.appcompat.widget.Toolbar

android:id="@+id/toolbar"

android:layout_width="409dp"

android:layout_height="wrap_content"

android:background="@color/material_dynamic_primary60"

android:minHeight="?attr/actionBarSize"

android:theme="?attr/actionBarTheme"

tools:layout_editor_absoluteX="1dp"

tools:layout_editor_absoluteY="4dp" />

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Calculadora"

android:textColor="@color/white"

android:textSize="34sp"

android:textStyle="bold"

tools:layout_editor_absoluteX="28dp"

tools:layout_editor_absoluteY="16dp" />

<EditText

android:id="@+id/num1"

android:layout_width="102dp"

android:layout_height="53dp"
android:layout_marginStart="28dp"

android:layout_marginTop="55dp"

android:layout_marginEnd="281dp"

android:layout_marginBottom="20dp"

android:hint="Número 1"

app:layout_constraintBottom_toTopOf="@+id/num2"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="1.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/toolbar" />

<EditText

android:id="@+id/num2"

android:layout_width="103dp"

android:layout_height="wrap_content"

android:layout_marginStart="32dp"

android:layout_marginTop="58dp"

android:layout_marginEnd="281dp"

android:layout_marginBottom="453dp"

android:hint="Número 2"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="1.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/num1" />

<Button

android:id="@+id/sumar1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="166dp"

android:layout_marginTop="358dp"

android:layout_marginEnd="155dp"

android:layout_marginBottom="257dp"
android:background="@color/material_dynamic_primary60"

android:backgroundTint="@color/material_dynamic_primary60"

android:backgroundTintMode="src_over"

android:text="Sumar"

android:textColorHighlight="@color/material_dynamic_primary60"

android:textColorLink="@color/material_dynamic_primary60"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/toolbar" />

<TextView

android:id="@+id/resultado"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="176dp"

android:layout_marginTop="260dp"

android:layout_marginEnd="177dp"

android:layout_marginBottom="79dp"

android:textSize="34sp"

app:layout_constraintBottom_toTopOf="@+id/button"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/toolbar" />

</androidx.constraintlayout.widget.ConstraintLayout>

COLORS:
<?xml version="1.0" encoding="utf-8"?>

<resources>

<color name="black">#FF000000</color>

<color name="white">#FFFFFFFF</color>

</resources>
Tarea UT03.2. Sumar números con botón programático.

MainActivity:
package com.example.tare22;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

//SE DECLARAN LAS VARIABLES

EditText numero1,numero2;

Button sumar,restar,multiplicar, dividir;

TextView resultado;

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//SE ASIGNAN LOS VALORES CON EL XML

numero1=findViewById(R.id.num1);

numero2=findViewById(R.id.num2);

sumar=findViewById(R.id.sumar1);

restar=findViewById(R.id.restar1);

multiplicar=findViewById(R.id.multiplicar1);

dividir=findViewById(R.id.dividir1);

resultado=findViewById(R.id.resultado1);

sumar.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

try {

double nume1 = Double.parseDouble(numero1.getText().toString());

double nume2 = Double.parseDouble(numero2.getText().toString());

double resultado1 = nume1 + nume2;

resultado.setText("Resultado: " + resultado1);

} catch (NumberFormatException e) {

resultado.setText("Error: Ingresa números válidos");

});

restar.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

try {

double nume1 = Double.parseDouble(numero1.getText().toString());

double nume2 = Double.parseDouble(numero2.getText().toString());

double resultado1 = nume1 - nume2;

resultado.setText("Resultado: " + resultado1);

} catch (NumberFormatException e) {
resultado.setText("Error: Ingresa números válidos");

});

multiplicar.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

try {

double nume1 = Double.parseDouble(numero1.getText().toString());

double nume2 = Double.parseDouble(numero2.getText().toString());

double resultado1 = nume1 * nume2;

resultado.setText("Resultado: " + resultado1);

} catch (NumberFormatException e) {

resultado.setText("Error: Ingresa números válidos");

});

dividir.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

try {

double nume1 = Double.parseDouble(numero1.getText().toString());

double nume2 = Double.parseDouble(numero2.getText().toString());

double resultado1 = nume1 / nume2;

resultado.setText("Resultado: " + resultado1);

} catch (NumberFormatException e) {

resultado.setText("Error: Ingresa números válidos");

});

Activity Main:
<?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="wrap_content"

android:layout_height="wrap_content"

tools:context=".MainActivity">

<TextView

android:id="@+id/resultado1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="20sp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.498"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.406" />

<androidx.appcompat.widget.Toolbar

android:id="@+id/toolbar"

android:layout_width="413dp"

android:layout_height="90dp"

android:background="?attr/colorPrimary"

android:minHeight="?attr/actionBarSize"

android:theme="?attr/actionBarTheme"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintStart_toStartOf="parent"

tools:layout_editor_absoluteY="-2dp" />
<Button

android:id="@+id/sumar1"

android:layout_width="110dp"

android:layout_height="60dp"

android:layout_marginStart="86dp"

android:layout_marginTop="35dp"

android:layout_marginEnd="45dp"

android:layout_marginBottom="13dp"

android:text="Sumar"

app:layout_constraintBottom_toTopOf="@+id/dividir1"

app:layout_constraintEnd_toStartOf="@+id/restar1"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/resultado1" />

<Button

android:id="@+id/restar1"

android:layout_width="110dp"

android:layout_height="60dp"

android:layout_marginStart="20dp"

android:layout_marginTop="35dp"

android:layout_marginEnd="98dp"

android:layout_marginBottom="14dp"

android:text="Restar"

app:layout_constraintBottom_toTopOf="@+id/multiplicar1"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toEndOf="@+id/sumar1"

app:layout_constraintTop_toBottomOf="@+id/resultado1" />

<Button

android:id="@+id/dividir1"

android:layout_width="110dp"

android:layout_height="60dp"

android:layout_marginStart="86dp"

android:layout_marginTop="13dp"
android:layout_marginEnd="45dp"

android:layout_marginBottom="211dp"

android:text="Div"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toStartOf="@+id/multiplicar1"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/sumar1" />

<Button

android:id="@+id/multiplicar1"

android:layout_width="110dp"

android:layout_height="60dp"

android:layout_marginStart="20dp"

android:layout_marginTop="14dp"

android:layout_marginEnd="98dp"

android:layout_marginBottom="211dp"

android:text="Mult"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toEndOf="@+id/dividir1"

app:layout_constraintTop_toBottomOf="@+id/restar1" />

<TextView

android:id="@+id/titulo2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="16dp"

android:layout_marginTop="16dp"

android:text="Tarea2"

android:textColor="@color/white"

android:textColorHint="@color/white"

android:textSize="34sp"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />
<EditText

android:id="@+id/num1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="16dp"

android:layout_marginTop="49dp"

android:hint="num1"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/toolbar" />

<EditText

android:id="@+id/num2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="16dp"

android:layout_marginTop="108dp"

android:hint="num2"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/toolbar" />

</androidx.constraintlayout.widget.ConstraintLayout>

COLORS:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>

You might also like