• 闰年测试非法输入的处理 简单安卓app 20150406


      在软件测试的课上,老师介绍了闰年测试。闰年测试旨在检测某一年份是否为闰年,计算方式为四年一闰,百年不闰,四百年再闰。使用安卓实现这个小程序。

      界面代码如下:

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5 
     6     tools:context="${relativePackage}.${activityClass}" >
     7 
     8     <TextView
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:layout_centerHorizontal="true"
    12         android:text="@string/hello_world" />
    13 
    14    <EditText
    15         android:id="@+id/editText"
    16         android:layout_width="fill_parent"
    17         android:layout_height="wrap_content"
    18         android:layout_marginTop="23dp"
    19         android:ems="10"
    20         android:hint="@string/hint"
    21         android:lines="1"
    22         >
    23     </EditText>
    24 
    25    <Button
    26        android:id="@+id/button"
    27        android:layout_width="wrap_content"
    28        android:layout_height="wrap_content"
    29        android:layout_below="@+id/editText"
    30        android:layout_centerHorizontal="true"
    31        android:text="@string/bt" />
    32 
    33 </RelativeLayout>

    结果如下:

      在我的程序中,使用Integer.parseInt的方法获取editText中输入的年份。在没考虑非法字符的情况下,输入特殊字符而非年份。程序停止运行。

    在android ADT的logcat中,可以看到程序运行的日志:

     String转化为int类型失败。考虑非法字符,即输入年份不合法,无法转换成String类型,使用try,catch截获错误,最终代码如下

     1 package com.leap.leapyear;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.view.Gravity;
     6 import android.view.View;
     7 import android.view.View.OnClickListener;
     8 import android.widget.Button;
     9 import android.widget.EditText;
    10 import android.widget.Toast;
    11 
    12 public class MainActivity extends Activity {
    13     private EditText mEditText;
    14 
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.activity_main);
    19 
    20         Button button = (Button) this.findViewById(R.id.button);
    21         mEditText = (EditText) MainActivity.this.findViewById(R.id.editText);
    22 
    23         button.setOnClickListener(new OnClickListener() {
    24             @Override
    25             public void onClick(View v) {
    26                 String number = mEditText.getText().toString();
    27                 checkLeap(number);
    28             }
    29         });
    30     }
    31 
    32     private void checkLeap(String number) {
    33 
    34         int messageResId = R.string.false_toast;
    35 
    36         try {
    37             int year = Integer.parseInt(number);
    38 
    39             if (year % 4 == 0) {
    40                 messageResId = R.string.true_toast;
    41             } 
    42             if (year % 100 == 0) {
    43                 messageResId = R.string.false_toast;
    44             }
    45             if (year % 400 == 0) {
    46                 messageResId = R.string.true_toast;
    47             }
    48         }
    49         catch (Exception e) {
    50             messageResId = R.string.illegal_toast;
    51         }
    52         
    53         Toast toast = Toast.makeText(this, messageResId, Toast.LENGTH_SHORT);
    54         toast.setGravity(Gravity.CENTER, 0, 0);
    55         toast.show();
    56     }
    57 }

    使用测试用例进行测试

    有效等价类:被4整除而且不被100整除或可以被400整除

    无效等价类:其他年份,非法字符,空

    测试结果如下

    测试结果:

    其他:

    有更简单的问题解决办法,在输入框<EditText/>下加一行android:inputType="number",限制输入为数字,测试发现点击输入框输入法自动切换为数字输入,而且无法输入除数字以外的字符。如下:

  • 相关阅读:
    [十七]SpringBoot 之 使用自定义的properties
    【转】手摸手,带你用vue撸后台 系列三(实战篇)
    【转】手摸手,带你用vue撸后台 系列四(vueAdmin 一个极简的后台基础模板)
    【转】手摸手,带你用vue撸后台 系列二(登录权限篇)
    【转】手摸手,带你用vue撸后台 系列一
    【16】vuex2.0 之 getter
    【15】vuex2.0 之 modules
    【14】vuex2.0 之 mutation 和 action
    【13】vuex2.0 之 state
    【12】vue-router 之路由重定向
  • 原文地址:https://www.cnblogs.com/wushaobo/p/4396308.html
Copyright © 2020-2023  润新知