0% found this document useful (0 votes)
7 views15 pages

Tarea 3 Botones

The document presents a mobile development project by Josué Ricardo Hernández Montero, including XML and Java code for a simple Android application. The app features a user interface with input fields for name and numbers, as well as buttons for unit conversions (length, weight, temperature). It also includes functionality to display greetings and handle input validation for conversions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Tarea 3 Botones

The document presents a mobile development project by Josué Ricardo Hernández Montero, including XML and Java code for a simple Android application. The app features a user interface with input fields for name and numbers, as well as buttons for unit conversions (length, weight, temperature). It also includes functionality to display greetings and handle input validation for conversions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRESENTACIÓN.

Participante:

Josué Ricardo Hernández Montero - 2024-0235

Fecha: 19/01/20256

Asignatura:

Introduccion al desarrollo movile

Docente:

Freidy Nunez Perez


Codigo XML:

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

<[Link]

xmlns:android="[Link]

xmlns:tools="[Link]

xmlns:app="[Link]

android:id="@+id/main"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<LinearLayout

xmlns:android="[Link]

android:orientation="vertical"

android:padding="16dp"

android:layout_width="match_parent"

android:layout_height="match_parent">

<!-- Nombre mostrado arriba -->

<TextView

android:id="@+id/tvGreeting"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Bienvenido"

android:textSize="20sp"

android:gravity="center"

android:padding="8dp"/>

<!-- Campo para ingresar nombre -->

<EditText

android:id="@+id/etName"
android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Ingresa tu nombre"/>

<!-- Display de números y resultados -->

<TextView

android:id="@+id/tvDisplay"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="0"

android:textSize="24sp"

android:gravity="end"

android:padding="16dp"

android:background="#DDDDDD"

android:layout_marginTop="12dp"/>

<!-- Teclado numérico -->

<GridLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:columnCount="3"

android:layout_marginTop="16dp">

<Button android:id="@+id/btn7" android:text="7"/>

<Button android:id="@+id/btn8" android:text="8"/>

<Button android:id="@+id/btn9" android:text="9"/>

<Button android:id="@+id/btn4" android:text="4"/>

<Button android:id="@+id/btn5" android:text="5"/>

<Button android:id="@+id/btn6" android:text="6"/>


<Button android:id="@+id/btn1" android:text="1"/>

<Button android:id="@+id/btn2" android:text="2"/>

<Button android:id="@+id/btn3" android:text="3"/>

<Button android:id="@+id/btn0" android:text="0"/>

<Button android:id="@+id/btnClear" android:text="Reiniciar"/>

</GridLayout>

<!-- Botones de conversión -->

<LinearLayout

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="16dp">

<Button

android:id="@+id/btnLength"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Metros ↔ Pies"/>

<Button

android:id="@+id/btnWeight"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Kg ↔ Libras"/>

<Button

android:id="@+id/btnTemp"
android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Celsius ↔ Fahrenheit"/>

</LinearLayout>

</LinearLayout>

</[Link]>

Codigo Java:

package [Link].tarea2;

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

private TextView tvGreeting;

private EditText etName;

private TextView tvDisplay;


private Button btnLength;

private Button btnWeight;

private Button btnTemp;

private Button btnClear;

private List<Button> numberButtons = new ArrayList<>();

@Override

protected void onCreate(Bundle savedInstanceState) {

[Link](savedInstanceState);

setContentView([Link].activity_main);

tvGreeting = findViewById([Link]);

etName = findViewById([Link]);

tvDisplay = findViewById([Link]);

btnLength = findViewById([Link]);

btnWeight = findViewById([Link]);

btnTemp = findViewById([Link]);

btnClear = findViewById([Link]);

int[] ids = {

[Link].btn0, [Link].btn1, [Link].btn2, [Link].btn3,

[Link].btn4, [Link].btn5, [Link].btn6,

[Link].btn7, [Link].btn8, [Link].btn9

};

for (int id : ids) {

[Link](findViewById(id));
}

disableConversionButtons();

[Link](new TextWatcher() {

@Override

public void afterTextChanged(Editable s) {

String name = [Link]().trim();

if (![Link]()) {

[Link]("Hola, " + name);

enableConversionButtons();

} else {

[Link]("Bienvenido");

disableConversionButtons();

@Override

public void beforeTextChanged(CharSequence s, int start, int


count, int after) {}

@Override

public void onTextChanged(CharSequence s, int start, int before,


int count) {}

});

for (Button button : numberButtons) {

[Link](v -> {

Button b = (Button) v;

appendNumber([Link]().toString());

});
}

[Link](v -> [Link]("0"));

[Link](v -> convertLength());

[Link](v -> convertWeight());

[Link](v -> convertTemp());

private void appendNumber(String number) {

String current = [Link]().toString();

if ([Link]("0")) {

[Link](number);

} else {

[Link](current + number);

private Double getInputValue() {

try {

return [Link]([Link]().toString());

} catch (Exception e) {

return null;

private void convertLength() {

Double value = getInputValue();

if (value == null) {

showError();
return;

double result = value * 3.28084;

[Link]([Link]("%.2f", result));

private void convertWeight() {

Double value = getInputValue();

if (value == null) {

showError();

return;

double result = value * 2.20462;

[Link]([Link]("%.2f", result));

private void convertTemp() {

Double value = getInputValue();

if (value == null) {

showError();

return;

double result = (value * 9 / 5) + 32;

[Link]([Link]("%.2f", result));

private void showError() {

[Link](this, "Ingresa un número válido",


Toast.LENGTH_SHORT).show();

}
private void disableConversionButtons() {

[Link](false);

[Link](false);

[Link](false);

private void enableConversionButtons() {

[Link](true);

[Link](true);

[Link](true);

}
Capturas:​

Metros - pies:
Kg - Libras:
Celsius - Fahrenheit:

You might also like