Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android两个activity传值

Android开发两个activity之间传值示例详解

作者:AaVictory.

这篇文章主要为大家介绍了Android开发两个activity之间传值示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

使用Inten的putExtra传递

//创建意图对象
 Intent intent = new Intent(this,MainActivity2.class);
 //设置传递键值对
 intent.putExtra("name","花花");
 //激活意图
 startActivity(intent);
// 获取意图对象
 Intent intent = getIntent();
 //获取传递的值
 String name= intent.getStringExtra("name");
 //设置值
 tv.setText(name);

使用Intention的Bundle传递

 Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                Bundle bundle=new Bundle();
                bundle.putInt("id",2);
                bundle.putString("name",ss);
                intent.putExtra("bundle",bundle);
                startActivity(intent);
Intent intent=getIntent();
        Bundle bundle=intent.getBundleExtra("bundle");
        final int id = bundle.getInt("id");
        final String name = bundle.getString("name");
        tv.setText(id);
        tv2.setText(name);

使用Activity销毁时传递数据

  Intent intent = new Intent(MainActivity.this,TwoActivity.class);
  //用一种特殊方式开启Activity
 startActivityForResult(intent, 11);
 //设置数据
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 String str = data.getStringExtra("data");
 tvOne.setText(str);
}
//设置返回的数据
 Intent intent = new Intent();
 intent.putExtra("data", edtOne.getText().toString().trim());
 setResult(3, intent);
 //关闭当前activity
 finish();

SharedPreferences传递数据

 SharedPreferences sp=getSharedPreferences("com.example.data4",0);
                final SharedPreferences.Editor edit = sp.edit();
                edit.putString("name",ss);
                edit.putString("id","8");
                edit.apply();
                Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                startActivity(intent);
SharedPreferences sp=getSharedPreferences("com.example.data4",0);
        sp.getString("name","");
        tv1.setText(sp.getString("id",""));
        tv2.setText(sp.getString("name",""));

使用序列化对象Seriazable

import java.io.Serializable;
class DataBean implements Serializable {
 private String name;
 private String sex;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public String getSex() {
 return sex;
 }
 public void setSex(String sex) {
 this.sex = sex;
 }
}
//创建意图
 Intent intent = new Intent(MainActivity.this,TwoActivity.class);
 DataBean bean = new DataBean();
 //通过set方法把数据保存到DataBean对象中
 bean.setName("啦啦");
 bean.setSex("男");
 intent.putExtra("key", bean);
 startActivity(intent);
Intent intent = getIntent();
 //反序列化数据对象
 Serializable se = intent.getSerializableExtra("key");
 if(se instanceof DataBean){
  //获取到携带数据的DataBean对象db
  DataBean db = (DataBean) se;
  tv.setText(db.getName()+"==="+db.getSex());
 }

使用静态变量传递数据

Intent intent = new Intent(MainActivity.this,TwoActivity.class);
  TwoActivity.name="花花";
  TwoActivity.str="在做什么";
  startActivity(intent);
//静态变量
protected static String name;
protected static String str;
tv.setText(str+name);

handler

//点击跳转第二个页面
bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });

//创建一个handler,处理信息
 @SuppressLint("HandlerLeak")
    public static Handler mhandler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    Log.e("ddddddd","Dddddddd");
                    break;
            }
        }
    };
//点击按钮,返回第一个activity
 btt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handler=MainActivity.mhandler;
                Message message=new Message();
                message.what=1;
                handler.sendMessage(message);
            }
        });
//点击按钮,10秒后 ,返回第一个activity
btt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //开一个子线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                        //10秒之后发送
                            Thread.sleep(10000);
                            handler=MainActivity.mhandler;
                            Message message=new Message();
                            message.what=1;
                            handler.sendMessage(message);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
//点击按钮,每10秒 ,返回第一个activity一次(循环一次),一共返回10次
 btt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int i=0;
                        do {
                            try {
                                Thread.sleep(10000);
                                handler=MainActivity.mhandler;
                                Message message=new Message();
                                message.what=1;
                                handler.sendMessage(message);
                                i++;
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }while (i<10);
                    }
                }).start();
            }
        });

以上就是Android开发两个activity之间传值示例详解的详细内容,更多关于Android两个activity传值的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文