如何将文本从一个Activity发送到另一个Activity?

时间:2013-02-24 13:07:14

标签: android eclipse

calculated.java :(这必须命令显示calculated.xml布局)

public class Calculated extends Activity {


   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.calculated);

   }
}

以上:

主要课程:

 package com.barth.appie;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.EditText;


public class MainActivity extends Activity {


Button button1;
String text;


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

public void addListenerOnButton() {

    button1 = (Button) findViewById(R.id.buttoncalculate);
    button1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {

        EditText editText = (EditText)findViewById(R.id.editText1);
        String text = editText.getText().toString();

             Intent myIntent = new Intent(view.getContext(),    Calculated.class);
             startActivityForResult(myIntent, 0);

        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}



}

calculated.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"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:text="@string/text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

此外我在strings.xml处有<string name="text">Value</string>,而在另一个名为calculated.class的类中,我有它以便输出calculated.xml(工作正常)

我想要的:我想在calculated.xml中显示文本,这是一个在main.class中创建的字符串,我希望字符串是填充在文本字段中的文本,名为“editText1”

2 个答案:

答案 0 :(得分:2)

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.EditText;


public class MainActivity extends Activity {

Button button1;
String text;

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

public void addListenerOnButton() {

    button1 = (Button) findViewById(R.id.buttoncalculate);
    button1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {

        EditText editText = (EditText)findViewById(R.id.editText1);
        String text = editText.getText().toString();

             Intent myIntent = new Intent(view.getContext(),Calculated.class);
             myIntent.putExtra("mytext",text);
             startActivity(myIntent);

        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
  }
}

Calculated.java

public class Calculated extends Activity {

TextView mTextview;

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

       mTextview = (TextView)findViewById(R.id.textView1);

       mTextview.setText(getIntent().getStringExtra("mytext"));
}

<强> calculated.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"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

答案 1 :(得分:1)

  

将数据从一个活动发送到另一个活动

MainActivity.java是您希望将数据发送到另一个活动的第一个活动。

Intent myIntent = new Intent(view.getContext(), Calculated.class);
myIntent.putExtra("text", text);
startActivity(myIntent);

Calculated.java是第二个活动,它接收从MainActivity.java传递的任何意图数据

String text = myIntent.getStringExtra("text");
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText(text);