android studio 单元测试和ui测试,在Android Studio中进行单元测试和UI测试 - 7.为app添加简单的交互...

系列教程

6002d7965f07

在使用Espresso进行UI测试前,让我们为app添加一些Views和简单的交互。我们使用一个用户可以输入名字的EditText,欢迎用户的Button和用于输出的TextView。打开res/layout/activity_main.xml,粘贴如下代码:

activity_main.xml

xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

android:id="@+id/textView"

android:text="@string/hello_world" android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:hint="Enter your name here"

android:id="@+id/editText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/textView"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Say hello!"

android:layout_below="@+id/editText"

android:onClick="sayHello"/>

还需要在MainActivity.java中添加onClick handler:

MainActivity.java

public void sayHello(View v){

TextView textView = (TextView) findViewById(R.id.textView);

EditText editText = (EditText) findViewById(R.id.editText);

textView.setText("Hello, " + editText.getText().toString() + "!");

}

现在可以运行app并确认一切工作正常。在点击Run按钮之前,确认你的Run Configuration没有设置为运行测试。如需更改,点击下拉选项,选择app。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值