The following is the Activity_mian code:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout 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"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= "Com.example.gxd.hellowordhw.MainActivity"
android:orientation= "Vertical" >
<textview
Android:id= "@+id/textview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Hello world!"/>
<edittext
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/edittext"/>
<button
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "SAY hello!"
Android:id= "@+id/button"
android:layout_gravity= "Center_horizontal"/>
</LinearLayout>
The following is the Mainactivity code:
package COM.EXAMPLE.GXD.HELLOWORDHW;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.EditText;
Import Android.widget.TextView;
public class Mainactivity extends Appcompatactivity {
@Override
protected void onCreate (Bundle s Avedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
}
public void SayHello (view view) {
TextView textview= (TextView) Findviewbyid (R.id.textview); br> EditText edittext= (EditText) Findviewbyid (R.id.edittext);
Textview.settext ("Hello," +edittext.gettext (). toString () + "!");
}
}
The following is the Mainactivitytext code:
public class Mainactivitytest {
private static final String string_to_be_typedto = "Peter";
@Rule
Public activitytestrule<mainactivity> mactivitytestrule = new activitytestrule<> (MainActivity.class);
@Test
public void Testsayhello () throws Exception {
Onview (Withid (R.id.edittext)). Perform (Testrule (string_to_be_typedto), Closesoftkeyboard);
Onview (Withid (R.id.button)). Perform (click ());
String expectedtext= "Hello," + String_to_be_typedto + "!";
Onview (Withid (R.id.textview)). Check (Matches (Withtext (Expectedtext)));
}
}
The following is the Build.gradle code:
Apply plugin: ' Com.android.application '
Android {
Compilesdkversion 23
Buildtoolsversion "24.0.2"
Defaultconfig {
ApplicationID "COM.EXAMPLE.GXD.HELLOWORDHW"
Minsdkversion 19
Targetsdkversion 23
Versioncode 1
Versionname "1.0"
Add for Test
Testinstrumentationrunner "Android.support.test.runner.AndroidJUnitRunner"
}
Buildtypes {
Release {
Minifyenabled false
Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro '
}
}
}
dependencies {
Compile Filetree (dir: ' Libs ', include: [' *.jar '])
Testcompile ' junit:junit:4.12 '
Compile ' com.android.support:appcompat-v7:23.4.0 '
Add for Test
Androidtestcompile ' com.android.support.test.espresso:espresso-core:2.2.2 ', {
Exclude group: ' Com.android.support ', module: ' Support-annotations '
}
}
Layout:
Unit testing in Android Studio