Android Intent多种传值方式详解

Android中提供了Intent机制来协助应用间的交互与通讯,或者采用更准确的说法是,Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互,传值的方式通过key-value传值。

Intent传参数:

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtn=(Button) findViewById(R.id.mBtn);
        mBtn.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        Intent intent=new Intent(MainActivity.this, ShowActivity.class);
        intent.putExtra("name", "我是小灰灰呀");
        startActivity(intent);
    }
}

在接收方,直接通过key值,就会接收到value,然后把value值直接设置在TextView上

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ShowActivity extends Activity {
    private TextView mTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
        mTv=(TextView) findViewById(R.id.mTv);
        String string = getIntent().getStringExtra("name");
        mTv.setText(string);
    }
}

Intent传递对象:

传递对象必须实现Serializable接口进行序列化,用来标明某个类可以被序列化,通过序列化可以把对象转化为与平台无关的二进制流,在重新使用前进行反序列化,重新转化为java对象。

import java.io.Serializable;

public class DogInfo implements Serializable {
    private String name;
    private String age;
    private String sex;

    public DogInfo(String name, String age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

}

这个实现功能也是通过点击事件,把值设置到跳转到的Activity页面,首先通过Bundle把对象装进去(就像快递一样,先把东西打包,然后装车,只有到了地方,才可以取),然后通过putSerializable序列化对象,最后再通过另一个页面的反序列化取。

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mBtn;
    DogInfo info=new DogInfo("阿拉斯加", "2", "男");//info 对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtn=(Button) findViewById(R.id.mBtn);
        mBtn.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        intent.setClass(MainActivity.this, ShowActivity.class);
        //bundle 用来储存数据 (K,V)
        Bundle bundle=new Bundle();
        bundle.putSerializable("info", info);
        intent.putExtra("bundle", bundle);
        startActivity(intent);
    }
}

这个是接收方,接收方式方传过来的对象,然后设置到接收方的TextView上,就是通过反序列化强制转换为已知的目标类,这样对象就轻松传过来了。

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

public class ShowActivity extends Activity {
    private TextView mTv_name,mTv_age,mTv_sex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
        initView();
        //获取Intent
        Intent intent = getIntent();
        //通过Key值获取bundle
        Bundle bundle = intent.getBundleExtra("bundle");
        //获取传送过来的Dog对象
        //    DogInfo info = (DogInfo) bundle.get("info");
        DogInfo info= (DogInfo) bundle.getSerializable("info");
        //给控件设置内容
        mTv_name.setText(info.getName());
        mTv_age.setText(info.getAge());
        mTv_sex.setText(info.getSex());
    }
    private void initView() {
        mTv_name=(TextView) findViewById(R.id.mTv_name);
        mTv_age=(TextView) findViewById(R.id.mTv_age);
        mTv_sex=(TextView) findViewById(R.id.mTv_sex);
    }
}

传图片:

下面分享一下效果图:

点击前                                                                      


点击后


activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/mBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bitmap本地传值" />
</RelativeLayout>

show.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/mIv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY" />
</LinearLayout>
在这个页面放置了一张图片,然后点击Button按钮跳转到一个页面,MainActivity页面的图片就会传到跳转到的页面,然后在跳转到的页面设置就行了,我是在MainActivity获取的一张图片,然后赋值给了bitmap,然后把bitmap传给跳转到的页面。
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/*
 * 1、background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸。src是图片内容(前景),
 * bg是背景,可以同时使用。
 * 2、scaleType只对src起作用;bg可设置透明度,比如在ImageButton中就可以用android:scaleType控制图片的缩放方式。
 * */
public class MainActivity extends Activity implements OnClickListener {
    private Button mBtn;
    private Bitmap bitmap;//android图片操作工具类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //怎么获得Bitmap  BitmapFactory查找资源
        bitmap=BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        mBtn=(Button) findViewById(R.id.mBtn);
        mBtn.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        intent.setClass(MainActivity.this, ShowActivity.class);
        intent.putExtra("bitmap", bitmap);
        startActivity(intent);
    }
}

主要是通过getParcelableExtra方法获得的

下面是这个方法的源码:可以看到方法里面存放的是一个String类型的数据,然后通过Parcelable接口,如果空的话就返回获得的数据。

  public <T extends Parcelable> T getParcelableExtra(String name) {

        return mExtras == null ? null : mExtras.<T>getParcelable(name);
    }
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;

public class ShowActivity extends Activity{
    private ImageView mImg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
        mImg=(ImageView) findViewById(R.id.mImg);
        //判断是否为空 接受 Intent
        if (getIntent()!=null) {
            Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
            mImg.setImageBitmap(bitmap);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/nazicsdn/article/details/79793655