我开发一个Android应用程序,找到年龄由用户提供的出生日期..三个编辑,文本是一个人也没有了一天,另外两个为月份和年份。我得到了code从这个链接。但我不知道下一步该怎么做。 ..我给了code到目前为止,我创建了......请您检查,并帮助我...
main_activity.xml
< RelativeLayout的的xmlns:机器人=http://schemas.android/apk/res/android 的xmlns:工具=http://schemas.android/tools 机器人:layout_width =match_parent 机器人:layout_height =match_parent 工具:上下文=MainActivity。> <的EditText 机器人:ID =@ + ID / editText1 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:layout_alignParentRight =真 机器人:layout_alignParentTop =真 机器人:layout_marginRight =32dp 机器人:layout_marginTop =42dp 机器人:EMS =10 机器人:inputType =约会> <不是requestFocus /> < /的EditText> <的EditText 机器人:ID =@ + ID / editText2 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:layout_alignRight =@ + ID / editText1 机器人:layout_below =@ + ID / editText1 机器人:layout_marginTop =30dp 机器人:EMS =10 机器人:inputType =日期/> <的EditText 机器人:ID =@ + ID / editText3 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:layout_alignRight =@ + ID / editText2 机器人:layout_below =@ + ID / editText2 机器人:layout_marginTop =24dp 机器人:EMS =10 机器人:inputType =日期/> <按钮 机器人:ID =@ + ID /按钮1 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:layout_below =@ + ID / editText3 机器人:layout_centerHorizontal =真 机器人:layout_marginTop =82dp 机器人:的onClick =getAge 机器人:文本=按钮/> <的TextView 机器人:ID =@ + ID / textView1 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:layout_alignLeft =@ + ID /按钮1 机器人:layout_below =@ + ID /按钮1 机器人:layout_marginTop =54dp 机器人:文本=TextView的/> < / RelativeLayout的>
MainActivity.java
公共类MainActivity延伸活动{ 一个长= 0; @覆盖 保护无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main); EditText上ET1 =(EditText上)findViewById(R.id.editText1); ET2的EditText =(EditText上)findViewById(R.id.editText2); ET3的EditText =(EditText上)findViewById(R.id.editText3); 按钮BTN1 =(按钮)findViewById(R.id.button1); TextView的TV1 =(TextView中)findViewById(R.id.textView1); } 公众诠释getAge(INT _year,诠释_month,诠释_day){ GregorianCalendar的CAL =新的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); 一= Y - cal.get(Calendar.YEAR); 如果((M< cal.get(Calendar.MONTH)) || ((米== cal.get(Calendar.MONTH))及及(D&其中;校准 获得(Calendar.DAY_OF_MONTH)))){ - 一个; } 如果(一个℃,) 抛出新抛出:IllegalArgumentException(时代℃的); 返回; } }解决方案
您应该添加一个点击监听到你的按钮,然后在里面,计算年龄和你的TextView中显示出来。
公共类MainActivity延伸活动{ 一个长= 0; @覆盖 保护无效的onCreate(包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); 按钮BTN1 =(按钮)findViewById(R.id.button1); 最后的TextView TV1 =(TextView中)findViewById(R.id.textView1); btn1.setOnClickListener(新OnClickListener { @覆盖 公共无效的onClick(视图v){ INT天=的Integer.parseInt(et1.getText()的toString()); INT月=的Integer.parseInt(et2.getText()的toString()); INT年=的Integer.parseInt(et3.getText()的toString()); tv1.setText(将String.valueOf(MainActivity.this.getAge(年,月,日))); } }); } 公众诠释getAge(INT _year,诠释_month,诠释_day){ GregorianCalendar的CAL =新的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); 一= Y - cal.get(Calendar.YEAR); 如果((M< cal.get(Calendar.MONTH)) || ((米== cal.get(Calendar.MONTH))及及(D&其中;校准 获得(Calendar.DAY_OF_MONTH)))){ - 一个; } 如果(一个℃,) 抛出新抛出:IllegalArgumentException(时代℃的); 返回; } }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; } }解决方案
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; } }
更多推荐
计算年龄使用出生日期
发布评论