gpt4 book ai didi

java - 使用出生日期计算年龄

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:08:14 27 4
gpt4 key购买 nike

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

ma​​in_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 中。

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

}

关于java - 使用出生日期计算年龄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20091877/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com