使用出生日期计算年龄

编程入门 行业动态 更新时间:2024-10-27 02:18:43
本文介绍了使用出生日期计算年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个Android应用程序,以查找从用户提供的出生日期的年龄。三个编辑文本有一个为一天,另外两个月和年。我得到了这个链接的代码..但是我不知道下一步该怎么做...我给代码远远我创建了...请检查并帮助我...

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 code from this link.. But I dont know what to do next... I am giving the code so far I created... pls check and help me...

main_activity.xml

<RelativeLayout xmlns:android="schemas.android/apk/res/android" xmlns:tools="schemas.android/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="32dp" android:layout_marginTop="42dp" android:ems="10" android:inputType="date" > <requestFocus /> </EditText> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="30dp" android:ems="10" android:inputType="date" /> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginTop="24dp" android:ems="10" android:inputType="date" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText3" android:layout_centerHorizontal="true" android:layout_marginTop="82dp" android:onClick="getAge" android:text="Button" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="54dp" android:text="TextView" /> </RelativeLayout>

MainActivity.java

public class MainActivity extends Activity { long a =0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText et1 = (EditText) findViewById (R.id.editText1); EditText et2 = (EditText) findViewById (R.id.editText2); EditText et3 = (EditText) findViewById (R.id.editText3); Button btn1 = (Button) findViewById (R.id.button1) ; TextView tv1 = (TextView) findViewById (R.id.textView1); } 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; } }

推荐答案

您应该在按钮中添加一个点击监听器,然后在其中添加一个点击监听器,计算年龄,并在TextView中显示。

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; } }

更多推荐

使用出生日期计算年龄

本文发布于:2023-10-18 11:48:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504148.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出生日期   年龄

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!