代码作业

这是一个界面

代码如下:

<?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:orientation="vertical"
tools:context="com.example.administrator.myapplication.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="enter your name here"
android:ems="10"
android:id="@+id/editText" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SAY HELLO!"
android:id="@+id/btn1" />
</LinearLayout>

 主代码:

package com.example.administrator.myapplication;

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 {
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sayHello(View view){
TextView textView=(TextView)findViewById(R.id.textView);
EditText editText=(EditText)findViewById(R.id.editText);
textView.setText("Hello,"+editText.getText().toString()+"!");
}
}

测试代码:

package com.example.administrator.myapplication;

import android.support.test.rule.ActivityTestRule;

import org.junit.Rule;
import org.junit.Test;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.typeText;
import static org.junit.Assert.*;

/**
* Created by Administrator on 2017/3/16.
*/
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity>mActivityRule=new ActivityTestRule<>(MainActivity.class);
@Test
public void testSayHello() throws Exception {
onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED).closeSoftKeyboard());

onView(withId(R.id.btn)).perform(click());
String expectedText="Hello,"+STRING_TO_BE_TYPED+"!";
onView(withId(R.id.textview)).check(matches(withText(expectedText)));

}
}
build代码:
package com.example.administrator.myapplication;

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 {
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sayHello(View view){
TextView textView=(TextView)findViewById(R.id.textView);
EditText editText=(EditText)findViewById(R.id.editText);
textView.setText("Hello,"+editText.getText().toString()+"!");
}
}
posted @ 2017-03-21 18:14  小灿。  阅读(216)  评论(0编辑  收藏  举报