In the course of software testing, the teacher introduced the leap year test. Leap year test is designed to detect whether a year is a leap, calculated as four years a leap, a century does not leap, 400 years again leap. Use Android to achieve this applet.
The interface code is as follows:
1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5 6tools:context= "${relativepackage}.${activityclass}" >7 8<TextView9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" OneAndroid:layout_centerhorizontal= "true" Aandroid:text= "@string/hello_world"/> - -<EditText theAndroid:id= "@+id/edittext" -Android:layout_width= "Fill_parent" -android:layout_height= "Wrap_content" -android:layout_margintop= "23DP" +Android:ems= "10" -android:hint= "@string/hint" +android:lines= "1" A> at</EditText> - -<Button -Android:id= "@+id/button" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" inandroid:layout_below= "@+id/edittext" -Android:layout_centerhorizontal= "true" toandroid:text= "@string/bt"/> + -</RelativeLayout>
The results are as follows:
In my program, use the Integer.parseint method to get the year entered in EditText. Enter special characters rather than years without considering illegal characters. The program stops running.
In the Logcat of Android ADT, you can see the log of the program running:
string conversion to int type failed. Consider illegal characters, that is, the input year is not legal, cannot be converted to a string type, use Try,catch intercept error, the final code is as follows
1 Packagecom.leap.leapyear;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 Importandroid.view.Gravity;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 ImportAndroid.widget.EditText;Ten ImportAndroid.widget.Toast; One A Public classMainactivityextendsActivity { - PrivateEditText Medittext; - the @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); + -Button button = (Button) This. Findviewbyid (R.id.button); +Medittext = (EditText) mainactivity. This. Findviewbyid (r.id.edittext); A atButton.setonclicklistener (NewOnclicklistener () { - @Override - Public voidOnClick (View v) { -String number =Medittext.gettext (). toString (); - checkleap (number); - } in }); - } to + Private voidCheckleap (String number) { - the intMessageresid =R.string.false_toast; * $ Try {Panax Notoginseng intYear =integer.parseint (number); - the if(year% 4 = = 0) { +Messageresid =R.string.true_toast; A } the if(year% 100 = = 0) { +Messageresid =R.string.false_toast; - } $ if(Year% 400 = = 0) { $Messageresid =R.string.true_toast; - } - } the Catch(Exception e) { -Messageresid =R.string.illegal_toast;Wuyi } the -Toast toast = Toast.maketext ( This, Messageresid, toast.length_short); WuToast.setgravity (gravity.center, 0, 0); - toast.show (); About } $}
Testing with test Cases
Valid equivalence classes: divisible by 4 and not divisible by 100 or divisible by 400
Invalid equivalence class: Other years, illegal characters, empty
The test results are as follows
Test results:
Other:
There is a simpler solution to the problem, in the input box <EditText/> next add a line android:inputtype= "number", limit the input to digital, test found that click the input box IME automatically switch to digital input, and can not enter characters other than the number. As follows:
Leap year test illegal input handling simple Android app 20150406