calcular edad
Publicado por Charly (69 intervenciones) el 12/03/2017 20:49:06
Hola, estoy creando una aplicación sencillita para calcular la edad real y la edad mental (de broma) de una persona.
Me inicia bien el programa, pero a la hora de darle al botón de aceptar y tener que hacer los cálculos se detiene el programa.
Hace un tiempo hice esto mismo en Java y me funcionó y ahora lo he modificado para android, pero no se qué he hecho mal.
Este es el código xml:
Y este el del java:
No se si estan mal las operaciones o el toast.
Me inicia bien el programa, pero a la hora de darle al botón de aceptar y tener que hacer los cálculos se detiene el programa.
Hace un tiempo hice esto mismo en Java y me funcionó y ahora lo he modificado para android, pero no se qué he hecho mal.
Este es el código xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="charly.android.pc.tu_edad.TuedadActivity">
<TextView android:text="Nombre:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtNombre"
android:inputType="textCapSentences"/>
<TextView android:text="Fecha de nacimiento(dd/mm/aaaa):"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtFecha"
android:inputType="textCapSentences"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aceptar"
android:onClick="btnAceptar"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancelar"
android:onClick="btnCancelar"/>
</LinearLayout>
</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package charly.android.pc.tu_edad;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TuedadActivity extends AppCompatActivity{
EditText txtNombre;
EditText txtFecha;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tuedad);
txtNombre=(EditText)findViewById(R.id.txtNombre);
txtFecha=(EditText)findViewById(R.id.txtFecha);
}
public int edadReal(){
String [] fecha_nacimiento=txtFecha.toString().split("/");
int dia=Integer.parseInt(fecha_nacimiento[0]);
int mes=Integer.parseInt(fecha_nacimiento[1]);
int año=Integer.parseInt(fecha_nacimiento[2]);
Calendar edad=new GregorianCalendar(año,mes,dia);
Calendar hoy=Calendar.getInstance();
int años=hoy.get(Calendar.YEAR)-edad.get(Calendar.YEAR);
edad.add(Calendar.YEAR,años);
if(hoy.before(edad)){
años--;
}
return años;
}
public int edadMental(int años){
if(txtNombre.getText().equals("carlos")){
años=años+2;
}else if(txtNombre.equals("alejandro")){
años=años-3;
}else if(txtNombre.equals("raquel")){
años=años-9;
}else{
años=años+0;
}
return años;
}
public void btnAceptar(View view,int años){
Toast.makeText(this,txtNombre.getText().toString()+" tu edad REAL es de "+edadReal()+ " años. Y tu edad MENTAL es de "+edadMental(años)+" años.",Toast.LENGTH_LONG).show();
}
public void btnCancelar(View view){
Toast.makeText(this,"ADIOS!",Toast.LENGTH_LONG).show();
System.exit(0);
}
}
Valora esta pregunta


0