使用出生日期计算年龄 [英] Calculate the age using the date of birth

查看:172
本文介绍了使用出生日期计算年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个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="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/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;
    }

}

这篇关于使用出生日期计算年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆