Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

android - How to send text from one Activity to another Activity?

calculated.java: (this has to command to show the calculated.xml layout)

public class Calculated extends Activity {


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

   }
}

older:

main class:

 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: (this is what it has to show after the button press)

<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>

Furthermore I have <string name="text">Value</string> at strings.xml, and in the other class called calculated.class, I have it so that it outputs the calculated.xml ( which works fine)

What I want: I want to display text in calculated.xml, which is a string made in main.class, and i want the string to be the text filled in textfield called "editText1"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...