我正在开发一个应用程序,要求用户按下按钮,然后转到另一个活动。问题是我不能从那里继续下去。我希望能够模拟按下按钮并返回之前的活动。这是我的代码。
package com.example.guy.smsclassproject;
import android.os.Looper;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
/**
* Created by ksl130230 on 11/5/2015.
*/
public class DraftsActivityTest extends ActivityInstrumentationTestCase2<DraftsActivity> {
private DraftsActivity tester;
private EditText searchText;
private Button searchButton;
private DraftsDatabase draftsDatabase;
private MessageObject messageObject1;
private MessageObject messageObject2;
private MessageObject messageObject3;
Button[] draftButtons;
ArrayList<MessageObject> messagesToBeDisplayed;
public DraftsActivityTest() {
super(DraftsActivity.class);
}
@Override
@UiThreadTest
public void setUp() throws Exception {
super.setUp();
if (Looper.myLooper() == null)
{
Looper.prepare();
}
draftsDatabase = new DraftsDatabase();
draftsDatabase.clearData();
messageObject1 = new MessageObject("hi", "5554",null, true);
messageObject2 = new MessageObject("hi hi", "5555554",null, true);
messageObject3 = new MessageObject("sup", "5435555554",null, true);
draftsDatabase.addMessage(messageObject1);
draftsDatabase.addMessage(messageObject2);
draftsDatabase.addMessage(messageObject3);
tester = getActivity();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
searchText = (EditText) tester.findViewById(R.id.searchText);
searchButton = (Button) tester.findViewById(R.id.searchButton);
}
@SmallTest
@UiThreadTest
public void testSearch() {
searchText.setText("hij");
searchButton.performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("Messages with the word hi", 0, messagesToBeDisplayed.size());
searchText.setText("sup");
searchButton.performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("Messages with the word sup", 1, messagesToBeDisplayed.size());
searchText.setText("yo");
searchButton.performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("Messages with the word yo", 0, messagesToBeDisplayed.size());
searchText.setText("i");
searchButton.performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("Messages with the word i", 2, messagesToBeDisplayed.size());
}
@SmallTest
@UiThreadTest
public void testRedisplay()
{
assertNotNull(tester.draftButtons[0]);
tester.draftButtons[0].performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("Size of the list after deletion is 2", 2, messagesToBeDisplayed.size()); //presses the first button, which deletes it from the drafts
getActivity();
String buttonText0 = tester.draftButtons[0].getText().toString();
if(buttonText0.equals("5555554: hi hi")) assertSame("Text redisplayed on the first button", buttonText0, messageObject2.toString()); //gets the text of the current button, since the messageobject1 was in draftsButtons0 before, not it should have messageobject2
assertNotNull(tester.draftButtons[1]);
String buttonText1 = tester.draftButtons[1].getText().toString();
if(buttonText1.equals("5435555554: sup"))
assertSame("Text redisplayed on the second button", buttonText1, messageObject3.toString());
}
@SmallTest
@UiThreadTest
public void testMessageButtons()
{
assertNotNull(tester.draftButtons[0]);
tester.draftButtons[0].performClick();
//THE PROBLEM IS LOCATED HERE.
//As soon as I press the button, the app goes to another activity.
//I want it to go back from the activity.
assertNotNull(tester.draftButtons[1]);
tester.draftButtons[1].performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("The draftsDatabase now only contains 1 message", 1, messagesToBeDisplayed.size());
assertNotNull(tester.draftButtons[0]);
tester.draftButtons[0].performClick();
assertNull(draftsDatabase); //after you press all the buttons, the draftsDatabase should be empty because all the messages have been deleted
}
}
答案 0 :(得分:0)
这是我最近制作的应用程序代码。如果用户按下一个按钮,它将转到另一个活动,您将模拟按下按钮并返回上一个活动,我也做了一些修改,您可以将数据从文本视图传递到第二个活动
MainActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
Intent i = new Intent(this, Main2Activity.class);
final EditText inputText = (EditText) findViewById(R.id.inputText);
String mesg = inputText.getText().toString();
i.putExtra("mm",mesg );
startActivity(i);
}
}
Main2Activity
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Transition;
import android.transition.TransitionManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
ViewGroup RLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle data = getIntent().getExtras();
if(data == null){
return;
}
String mm = data.getString("mm");
final TextView textVieww = (TextView) findViewById(R.id.textVieww);
textVieww.setText(mm);
RLayout = (ViewGroup) findViewById(R.id.RLayout);
RLayout.setOnTouchListener(
new RelativeLayout.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
moveButton();
return true;
}
});
}
public void onClickk(View view) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.user.razaali.third" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2" >
</activity>
</application>
</manifest>
activity_main.xml中
<RelativeLayout 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: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:background="#53ff00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="First Screen"
android:id="@+id/textView"
android:layout_above="@+id/inputText"
android:layout_centerHorizontal="true"
android:layout_marginBottom="33dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onClick" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/inputText"
android:width="250dp"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp" />
</RelativeLayout>
activity_main2.xml
<RelativeLayout 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: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="com.example.razaali.third.Main2Activity"
android:background="#fff306"
android:id="@+id/RLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Secound Screen"
android:id="@+id/textVieww"
android:layout_above="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change"
android:id="@+id/button2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onClickk" />
</RelativeLayout>