Calculate the age using the date of birth

后端 未结 4 893
甜味超标
甜味超标 2020-12-30 12:47

I am developing an android app to find the age from the date of birth provided by user.. three edit-texts are there one for day and other two for month and year. I got the c

4条回答
  •  灰色年华
    2020-12-30 12:56

    You should add a click listener to your button, then in it, calculate the age and display it in your TextView.

    public class MainActivity extends Activity {
    
        long a =0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final EditText et1 = (EditText) findViewById (R.id.editText1);
            final EditText et2 = (EditText) findViewById (R.id.editText2);
            final EditText et3 = (EditText) findViewById (R.id.editText3);
            Button btn1  = (Button)   findViewById (R.id.button1)  ;
            final TextView tv1 = (TextView) findViewById (R.id.textView1);
    
            btn1.setOnClickListener(new OnClickListener{
                @Override
                public void onClick (View v){
                    int day = Integer.parseInt(et1.getText().toString());
                    int month = Integer.parseInt(et2.getText().toString());
                    int year = Integer.parseInt(et3.getText().toString());
    
                    tv1.setText(String.valueOf(MainActivity.this.getAge(year, month, day)));
                }
            });
        }
    
    
        public int getAge (int _year, int _month, int _day) {
    
            GregorianCalendar cal = new GregorianCalendar();
            int y, m, d, a;         
    
            y = cal.get(Calendar.YEAR);
            m = cal.get(Calendar.MONTH);
            d = cal.get(Calendar.DAY_OF_MONTH);
            cal.set(_year, _month, _day);
            a = y - cal.get(Calendar.YEAR);
            if ((m < cal.get(Calendar.MONTH))
                            || ((m == cal.get(Calendar.MONTH)) && (d < cal
                                            .get(Calendar.DAY_OF_MONTH)))) {
                    --a;
            }
            if(a < 0)
                    throw new IllegalArgumentException("Age < 0");
            return a;
        }
    
    }
    

提交回复
热议问题