Android 中Activity 之间传递参数

1.传递简单数据

在A Activity中

findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent i = new Intent(MainActivity.this,TheActivity.class);
//        对基础的数据类型进行传递
       i.putExtra("data","我是国人");
       startActivity(i);
      }
    });

在B Activity中接受数据

  tv =(TextView)findViewById(R.id.TheTextView);
    Intent i = getIntent();
    tv.setText(i.getStringExtra("data"));

这种传值就是传递基本的数据类型

2.传递数据 包Bundle

在A Activity中

 findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent i = new Intent(MainActivity.this,TheActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("name","qll");
        bundle.putInt("age",3);

//        i.putExtras(bundle);
//        另种传递方式
        i.putExtra("data",bundle);
        startActivity(i);
      }
    });
  }

在B Activity中接受数据     

 tv =(TextView)findViewById(R.id.TheTextView);
    editText = (EditText) findViewById(R.id.editText3);
    Intent i = getIntent();
//    Bundle date = i.getExtras();
//     接受方式不同
    Bundle date = i.getBundleExtra("data");
    tv.setText(String.format("name=%s,age=%d",date.getString("name"),date.getInt("age")));

这总传递方式类似iOS中传递字典数据类型过来 。

3.传递值对象

自定义一个User类,传递自定义类需要对类进行序列化

用Serializable进行序列化

这种方法只需要类实现Serializable接口就可以了

User 类

import java.io.Serializable;

public class User implements Serializable{

  private String name;
  private int age;

  public int getAge(){
    return age;
  }

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

  public String getName(){
    return name;
  }

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

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

在A Activity中   

  findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent i = new Intent(MainActivity.this,TheActivity.class);
        i.putExtra("user",new User("qll",24));
        startActivity(i);
      }
    });
  }

在B Activity中

 tv =(TextView)findViewById(R.id.TheTextView);
 User user = (User)i.getSerializableExtra("user");
 tv.setText(String.format("user info(name=%s,age=%d)",user.getName(),user.getAge()));

用Parcelable实现

同样的需要实现Parcelable接口

User 类

package com.example.wyhaiapple.transferdata1;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.ParcelableSpan;

public class User implements Parcelable{
  private String name;
  private int age;

  public int getAge(){
    return age;
  }

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

  public String getName(){
    return name;
  }

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

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

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(getName());
    dest.writeInt(getAge());
  }


  public static final Creator<User> CREATOR = new Creator<User>() {
    @Override
    public User createFromParcel(Parcel source) {
      return new User(source.readString(),source.readInt());
    }

    @Override
    public User[] newArray(int size) {
      return new User[size];
    }
  };
}

在A Activity中 与上面的相同

在B Activity中

 tv =(TextView)findViewById(R.id.TheTextView);
 User user = (User)i.getParcelableExtra("user");
 tv.setText(String.format("user info(name=%s,age=%d)",user.getName(),user.getAge()));

4.获取 Activity 的返回参数

在B Activity中

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_the);
    tv =(TextView)findViewById(R.id.TheTextView);
    editText = (EditText) findViewById(R.id.editText3);

    findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent i = new Intent();
        i.putExtra("data",editText.getText().toString());
        setResult(1,i);
        finish();
      }
    });
  }
}

在A Activity中

startActivityForResult(i,0);

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    textView.setText("返回的值:"+data.getStringExtra("data"));
  }

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

01-27 16:35