通过Intent在Activity|Service之间传递数据

activity_main.xml:




    
    

activity_address.xml:




    

    


ActivityMain.java:

package com.miao.tran;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView addr1 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addr1 = (TextView)findViewById(R.id.text1);
    }

    public void select(View view) {
        Intent intent = new Intent("address");
        startActivityForResult(intent,123);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 123 & resultCode == 789){
            String msg = data.getStringExtra("msg");
            addr1.setText(msg);
        }
    }
}


AddressActivity.java:

package com.miao.tran;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class AddressActivity extends AppCompatActivity {
    private EditText addr2 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        addr2 = (EditText)findViewById(R.id.text2);
    }

    public void back(View view) {
        Intent intent = getIntent();
        intent.putExtra("msg",addr2.getText().toString());
        setResult(789, intent);
        finish();
    }


}




你可能感兴趣的:(Android,activity,android,数据,Intent)