首页 > 代码库 > 在Activity间传递数据的四种方法及返回结果

在Activity间传递数据的四种方法及返回结果

1.通用方法

使用intent的PutExtra方法传入,用getExtra等方法获得数据

主类

Intent intent=new Intent(MainActivity.this,OtherActivity.class);                intent.putExtra("name", "张三");                intent.putExtra("age", 23);                intent.putExtra("address", "北京");                startActivity(intent);

other类

        textView=(TextView) findViewById(R.id.msg);        Intent intent=getIntent();        int age=intent.getIntExtra("age", 0);        String name=intent.getStringExtra("name");        String address=intent.getStringExtra("address");        textView.setText("age--->>"+age+"name-->>"+name+"address-->>"+address);

 

2.利用静态变量传递

直接在主类中设置other类中的数据,进而实现主类到other类间的数据传递

主类

Intent intent=new Intent();                intent.setClass(MainActivity.this,OtherActivity.class);                OtherActivity.age=23;                OtherActivity.name="jack";                startActivity(intent);

other类

public class OtherActivity extends Activity{    private TextView textView;    public static String name;    public static int age;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.other);        textView=(TextView) findViewById(R.id.msg);        textView.setText("-name-->>"+name+"-age-->>"+age);    }}

 

3.使用全局变量传递

创建myApp继承Application,通过设置Application中的数据,在Activity中传递

MyApp类

package com.example.intent4;import android.app.Application;public class MyApp extends Application{    public String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public void onCreate() {        super.onCreate();        setName("张三");    }}

主类

myApp=(MyApp) getApplication();                myApp.setName("jack");//修改之后的名称                Intent intent=new Intent(MainActivity.this,OtherActivity.class);                startActivity(intent);

other类

textView=(TextView) findViewById(R.id.msg);        myApp=(MyApp) getApplication();        textView.setText("-appname-->>"+myApp.getName());

 

4.使用剪切板(不太会用)

 5.返回结果

package com.example.intent5;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;//返回结果public class MainActivity extends Activity {    private EditText one,two,result;    private Button button;    private final static int REQUESTCODE=1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button=(Button) this.findViewById(R.id.calculate);        one=(EditText) findViewById(R.id.one);        two=(EditText) findViewById(R.id.two);        result=(EditText) findViewById(R.id.result);        button.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View v) {                int a=Integer.parseInt(one.getText().toString());                int b=Integer.parseInt(two.getText().toString());                Intent intent=new Intent(MainActivity.this,OtherActivity.class);                intent.putExtra("a", a);                intent.putExtra("b", b);                startActivityForResult(intent,REQUESTCODE);//可以返回结果            }        });    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        //用于接受回传过来的值        if(resultCode==2){            if(requestCode==REQUESTCODE){                int three=data.getIntExtra("three", 0);                result.setText(String.valueOf(three));                            }        }    }        }
package com.example.intent5;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class OtherActivity extends Activity {    private Button button;    private TextView textView;    private EditText editText;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.other);        button=(Button) this.findViewById(R.id.button1);        textView=(TextView) this.findViewById(R.id.msg);        editText=(EditText) findViewById(R.id.editText);        Intent intent=getIntent();        int a=intent.getIntExtra("a", 0);        int b=intent.getIntExtra("b", 0);        textView.setText(a+"+"+b+"="+"?");        button.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View v) {                Intent intent=new Intent();                int three=Integer.parseInt(editText.getText().toString());                intent.putExtra("three", three);                //通过Intent对象返回结果,setResult方法                setResult(2, intent);                finish();//结束当前Activity的生命周期            }        });    }}

注意:返回结果主类需要调用startForRexult方法及onActivityResult(),判断请求码及结果码是否正确,other类要写setResult(2, intent); finish();

在Activity间传递数据的四种方法及返回结果