频道栏目
IT货架 > > 正文
Android开发基础之Activity之间参数传递
网友分享于:Jan 1, 1970 8:00:00 AM    来源: IT货架   

标签:

一、简单数值传递

1、MainActivity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")));
                Intent i=new Intent(MainActivity.this,NewActivity.class);
                i.putExtra("data","我是要传递的字符串");
                startActivity(i);
            }
        });
    }
2、NewActivity

private TextView T;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        Intent i=getIntent();
        T=(TextView) findViewById(R.id.textView1);
        T.setText(i.getStringExtra("data"));
    }

二、数据包Bundle传递

1、MainActivity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(MainActivity.this,NewActivity.class);
                Bundle B=new Bundle();
                B.putString("name","张三");
                B.putInt("age",23);
                i.putExtra("data",B);
                startActivity(i);
            }
        });
    }
2、NewActivity
private TextView T;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        Intent i=getIntent();
        Bundle b=new Bundle();
        b=i.getBundleExtra("data");
        T=(TextView) findViewById(R.id.textView1);
        T.setText(String.format("name:%s",b.getString("name")));
    }




Android开发基础之Activity之间参数传递

标签:

相关板块推荐 iPhone WindowsPhone

广告服务联系QQ:1134687142 | 网站地图

版权所有: IT货架- 内容来自互联网,仅供用于技术学习,请遵循相关法律法规. 京ICP备11030978号-1