Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. Button btn;
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7.  
  8. btn = (Button) findViewById(R.id.btn);
  9.  
  10. btn.setOnClickListener(new View.OnClickListener() {
  11. @Override
  12. public void onClick(View v) {
  13. PegarDatos();
  14. }
  15. });
  16. }
  17.  
  18.  
  19. private void PegarDatos(){
  20. Calendar c = Calendar.getInstance();
  21. int dia = c.get(Calendar.DAY_OF_MONTH);
  22. final int mes = c.get(Calendar.MONTH);
  23. int año = c.get(Calendar.YEAR);
  24.  
  25. DatePickerDialog dpd = new DatePickerDialog(MainActivity.this, AlertDialog.THEME_HOLO_DARK, new DatePickerDialog.OnDateSetListener() {
  26. @Override
  27. public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
  28. Intent i = new Intent(MainActivity.this, DosActivity.class);
  29. i.putExtra("DiaN",dayOfMonth);
  30. i.putExtra("MesN",month);
  31. i.putExtra("AñoN",year);
  32.  
  33. btn.setText(dayOfMonth+"/"+(month+1)+"/"+year);
  34. }
  35. },año,mes,dia);
  36. c.add(Calendar.SECOND, 1);
  37. dpd.getDatePicker().setMaxDate(c.getTimeInMillis());
  38. dpd.getDatePicker().setMinDate(0);
  39. dpd.show();
  40. }
  41.  
  42. TextView tvEdad;
  43.  
  44. @Override
  45. protected void onCreate(Bundle savedInstanceState) {
  46. super.onCreate(savedInstanceState);
  47. setContentView(R.layout.activity_dos);
  48.  
  49. Bundle BN = this.getIntent().getExtras();
  50.  
  51. int d = BN.getInt("DiaN");
  52. int m = BN.getInt("MesN");
  53. int y = BN.getInt("AñoN");
  54. CalcularEdad(d,m,y);
  55. }
  56.  
  57. private void CalcularEdad (int diaNac, int mesNac, int anoNac){
  58. Calendar c = Calendar.getInstance();
  59. int diaAct = c.get(Calendar.DAY_OF_MONTH);
  60. int mesAct = c.get(Calendar.MONTH);
  61. int anoAct = c.get(Calendar.YEAR);
  62.  
  63. int edad = anoAct-anoNac;
  64. if ( mesNac>mesAct){
  65. edad--;
  66. }else if (mesAct==mesNac){
  67. if (diaNac>diaAct){
  68. edad--;
  69. }
  70. }
  71.  
  72. tvEdad = (TextView) findViewById(R.id.tvEdad);
  73. tvEdad.setText("Edad: " + edad + " años");
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement