如何将文本从一个活动显示到另一个活动?

时间:2017-03-25 08:22:22

标签: java android

我是Android编程的新手,我试图创建一个非常简单的功能。我想在输出活动中显示我在Main活动中输入的文本。我试过但是点击按钮后应用程序崩溃了。请帮帮我谢谢。

主要活动.java

public class MainActivity extends Activity implements View.OnClickListener  
{

private EditText name, email, surname;
private Button button;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    name = (EditText) findViewById(R.id.name);
    email = (EditText) findViewById(R.id.email);
    surname = (EditText) findViewById(R.id.surname);

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(MainActivity.this);

    result = (TextView) findViewById(R.id.result);
}

@Override
public void onClick(View view) {

    String t1 = name.getText().toString();
    String t2 = email.getText().toString();
    String t3 = surname.getText().toString();

    String t4 = t1+"\n"+t2+"\n"+t3;

    Intent intent = new Intent(MainActivity.this,output_activity.class);
    intent.putExtra("RESULT",t4);
    startActivity(intent);

}
}

output_activity.java

public class output_activity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.output);

    TextView res = (TextView) findViewById(R.id.outputresult);
    res.setText(getIntent().getStringExtra("RESULT"));
  }
}

主要活动文件

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="my.com.androidclass.testtext.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="15dp"
    android:id="@+id/name" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:layout_below="@+id/name"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="17dp"
    android:id="@+id/email" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:layout_below="@+id/email"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="17dp"
    android:id="@+id/surname" />

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/surname"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView"
    android:layout_marginTop="22dp"
    android:id="@+id/button" />

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button"
    android:layout_alignRight="@+id/button"
    android:layout_alignEnd="@+id/button"
    android:layout_marginTop="28dp"
    android:id="@+id/result" />

   </RelativeLayout>

的Output.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:text="TextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/outputresult" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

<强> Mainfest

<application>标记

中添加此内容
 <activity android:name=".output_activity"></activity>