Android -- Activity值传递

我们需要创建2个Activity,一个是A,一个是B。

简单的值传递

A的activity组织要传输给B的数据。
Intent i = new Intent(AMainActivity.this,BMainActivity.class);此行代码创建Intent对象,然后用于传输数据。

package com.example.testapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class AMainActivity extends Activity {

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

        findViewById(R.id.button1).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(AMainActivity.this,
                                BMainActivity.class);
                        // 传参数
                        i.putExtra("name", "gaopeng");
                        startActivity(i);
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.amain, menu);
        return true;
    }

}

B的Activity接收A传过来的数据,通过i.getStringExtra(“name”)获取数值。

package com.example.testapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class BMainActivity extends Activity {

    private TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmain);
        t = (TextView) findViewById(R.id.tv);
        Intent i = getIntent();
        t.setText(i.getStringExtra("name"));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bmain, menu);
        return true;
    }

}

数据传输包Bundle

A的Activity,代码中我们用到了Bundle类,有点类似我们java中的map集合的用法。
使用putExtras或者putExtra方法将数据包放入到传输对象中。两个方法区别,一个是直接放入,另外一个是将数据包起一个别名,类似于KV的存储格式。

package com.example.testapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class AMainActivity extends Activity {

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

        findViewById(R.id.button1).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(AMainActivity.this,
                                BMainActivity.class);
                        Bundle b = new Bundle();
                        b.putString("name", "gaopeng");
                        b.putInt("age", 28);
//                      i.putExtras(b);
                        i.putExtra("user", b);
                        startActivity(i);
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.amain, menu);
        return true;
    }

}

B的Activity,通过getBundleExtra或getExtras方法渠道Bundle对象,然后按照存放时候的名字,取出数值。

package com.example.testapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class BMainActivity extends Activity {

    private TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmain);
        t = (TextView) findViewById(R.id.tv);
        Intent i = getIntent();
        Bundle b = i.getBundleExtra("user");
//      Bundle b = i.getExtras();
        t.setText("name=" + b.getString("name") + ",age=" + b.getInt("age"));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bmain, menu);
        return true;
    }

}

传输对象

既然是传输对象,那么我们就需要创建一个对象类。
对象类需要实现序列化接口,这里我实现的是Serializable接口,Android有自带的序列化接口Parcelable,下一篇博文,我详细说明差异及用法。

package com.example.testapp;

import java.io.Serializable;

import android.os.Parcel;
import android.os.Parcelable;

public class User implements Serializable {
    // public class User implements Parcelable{

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    User(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

A的Activity,

package com.example.testapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class AMainActivity extends Activity {

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

        findViewById(R.id.button1).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(AMainActivity.this,
                                BMainActivity.class);
                        i.putExtra("user", new User("lijingwei", 25));
                        startActivity(i);
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.amain, menu);
        return true;
    }

}

B的Activity,通过getSerializableExtra获取对象。这种方式在我们以后的开发中应该会经常被用到。符合我们java面向对象的开发。

package com.example.testapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class BMainActivity extends Activity {

    private TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmain);
        t = (TextView) findViewById(R.id.tv);
        Intent i = getIntent();
        User user = (User) i.getSerializableExtra("user");
        t.setText("name=" + user.getName() + ",age=" + user.getAge());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bmain, menu);
        return true;
    }

}

附上运行时的截图

A的Activity界面
技术分享
B的Activity界面
技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。