Android-Intent实现数据传递

在activityA中使用putExtras(bundle)传递数据,在activityB中使用getExtras()获取数据

MainActivity.java及其xml

package com.example.intentactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button go;
    EditText txt1;

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

    public void go(View view){
        go = findViewById(R.id.go);
        txt1 = findViewById(R.id.text1);
        Intent intent = new Intent(this,MainActivity2.class);
        Bundle bundle = new Bundle();
        bundle.putString("txt1",txt1.getText().toString());
        intent.putExtras(bundle);
        startActivity(intent);
    }
}




    

    

Mainactivity2.java和xml

package com.example.intentactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

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

        TextView text = findViewById(R.id.text);
        Bundle receive = this.getIntent().getExtras();
        String text1 = receive.getString("txt1");
        text.setText(text1+", this is the passion");
    }
}



    

    


你可能感兴趣的:(Android,android)