How to send text from one Activity to another Activity?

后端 未结 2 1710
我寻月下人不归
我寻月下人不归 2020-12-15 14:50

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

public class Calculated extends Activity {


   public void onCreate(Bundle savedIns         


        
相关标签:
2条回答
  • 2020-12-15 15:16
    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>
    
    0 讨论(0)
  • 2020-12-15 15:18

    To send the data from one activity to another activity

    MainActivity.java is first activity from where you want to send data to another activity.

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

    Calculated.java is second activity which receive the intent data whatever you pass from MainActivity.java

    String text = myIntent.getStringExtra("text");
    TextView textView = (TextView)findViewById(R.id.textView1);
    textView.setText(text);
    
    0 讨论(0)
提交回复
热议问题