Android学习笔记之Activity间传递参数

(一)Activity跳转时如何传递简单数据
Activity间简单数据传输
myFirstActivity 与 mySecondActivity之间数据传输使用putExtra(String name ,“传输内容”),在mySecondActivity中使用getStringExtra(“传输数据内容的名称”)来接收;

Android Studio新建myFirstActivity.java


package com.sh.elliott.myactivitypar;

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

public class myFirstActivity extends AppCompatActivity {

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

        findViewById(R.id.secondbtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //生成Intent对象来进行Activity之间进行通信
                Intent i = new Intent(myFirstActivity.this , mySecondActivity.class);
                //调用putExtra()来进行数据传输
                i.putExtra("date" , "myFirstActivity传递给mySecondActivity的数据成功接收");
                //利用Intent对象i来启动mySecondActivity
                startActivity(i);
            }
        });

    }
}

Android Studio新建mySecondActivity.java

package com.sh.elliott.myactivitypar;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class mySecondActivity extends AppCompatActivity {

            private TextView  myscondTextview = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_second);

        myscondTextview = (TextView) findViewById(R.id.myScondActivty);
        ////调用getIntent()来进行数据接收并获取
        myscondTextview.setText(getIntent().getStringExtra("date"));
    }
}

manifest文件中的注册信息:
如果你使用的是ecplise则需要手动将Activity注册下,如使用Android Studio则不需要(悄悄告诉你,它会自动注册!!!)

 <application  android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".myFirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".mySecondActivity"></activity>
    </application>

layout目录下的配置文件这里就不上了,主要是太戳~~~

下面是效果:

点击Button后的效果:

这下就彻底明白了,恭喜你呀又学了一招!!!!

(二)Activity跳转时如何传递复杂的数据
Activity间复杂数据传输的方法
myFirstActivity 与 mySecondActivity之间数据传输使用Bundle的对象调用putString(String name ,“传输内容”)或者 put * ()方法,使用Intent对象调用putExtra()方法,并将自己的Bundle对象传入进去,在mySecondActivity中使用getIntent()对象来获取Intent对象i ,使用i 调用putExtras()来接收Bundle数据;
Android Studio新建myFirstActivity.java


package com.sh.elliott.myactivitypar;

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

public class myFirstActivity extends AppCompatActivity {

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

        findViewById(R.id.secondbtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //生成Intent对象来进行Activity之间进行通信
                Intent i = new Intent(myFirstActivity.this , mySecondActivity.class);
                //调用putExtra()来进行数据传输
               // i.putExtra("date" , "myFirstActivity传递给mySecondActivity的数据成功接收"); 

                //activity跳转时如何传递复杂数据
                Bundle b = new Bundle();
                b.putString("name" , "wecome to bakc !!");
                b.putInt("age" , 30);
               // b.putString("scores" , "faile");
                //intent对象i载着bundle的数据对象b传输到secondActivity;
                i.putExtras(b);
                //利用Intent对象i来启动mySecondActivity
                startActivity(i);
            }
        });

    }
}
**注意:如果有这句b.putString("scores" , "faile");则,scores的值不在是默认的"不及格" ,而是"faile" !!!**

Android Studio新建mySecondActivity.java

package com.sh.elliott.myactivitypar;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class mySecondActivity extends AppCompatActivity {

            private TextView  myscondTextview = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_second);

        Intent i = getIntent(); //在secondActivity 中获取到传递过来的firstActivity的Intent对象i
        Bundle date = i.getExtras(); //i调用getExtras()获取传递过来的数据并将它保存在Bundle对象date中;
        myscondTextview = (TextView) findViewById(R.id.myScondActivty);
        //定义数据显示格式
        myscondTextview.setText(String.format("name=%s , age = %d ,scores = %s" , date.getString("name") , date.getInt("age") , date.getString("scores" ,"不及格")));
         //调用getIntent()来进行数据接收并获取
        //myscondTextview.setText(getIntent().getStringExtra("date"));
    }
}

运行效果:

Android学习笔记之Activity间传递参数_第1张图片

点击Button后:

Android学习笔记之Activity间传递参数_第2张图片

到了这里,我表示你已经征服了Bundle复杂数据的传输了!!!

(三)Activity跳转时如何传递自定义的值对象;
在原来的基础上新建类suSchoole(),并实现它的setter(),getter()方法,以及构造方法;
1)使用java.io.Serializable;中的Serializable()方法;操作简单,由系统自动帮助你序列化;
2)使用import android.os.Parcelable;中的Parcelable()方法;操作比较灵活复杂,必须手动序列化;

<1> 使用java.io.Serializable;中的Serializable()方法来序列化的使用;   

suSchoole.java

package com.sh.elliott.myactivitypar;

import java.io.Serializable;

/** * Created by elliott on 15/12/9. */
public class suShoole implements Serializable{

    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;
    }

   public  suShoole(String name , int age){

        this.age=age;
        this.name=name;
    }
}

Android Studio新建myFirstActivity.java


package com.sh.elliott.myactivitypar;

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

public class myFirstActivity extends AppCompatActivity {

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

        findViewById(R.id.secondbtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //生成Intent对象来进行Activity之间进行通信
                Intent i = new Intent(myFirstActivity.this , mySecondActivity.class);

                //intent对象i调用putExtra()进行数据传输;
                i.putExtra("shoole" , new suShoole("elliott" , 18));

                //利用Intent对象i来启动mySecondActivity
                startActivity(i);
            }
        });

    }
}

Android Studio新建mySecondActivity.java

package com.sh.elliott.myactivitypar;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class mySecondActivity extends AppCompatActivity {

            private TextView  myscondTextview = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_second);

        myscondTextview = (TextView) findViewById(R.id.myScondActivty);
        //调用getIntent()来进行数据接收并获取
       // myscondTextview.setText(getIntent().getStringExtra("date"));

        //使用java输入输流工具包中的Serializable来对象序列化,并使用getSerializableExtra()来获取数据;
       suShoole shoole = (suShoole) i.getSerializableExtra("shoole");
       //定义获取数据的格式;
       myscondTextview.setText(
           String.format(" shoole info(name=%s,age=%d)" ,
           shoole.getName(), 
           shoole.getAge())
           );

        }
    }


执行效果:




![这里写图片描述](http://img.blog.csdn.net/20151209161745992)

点击Button
![这里写图片描述](http://img.blog.csdn.net/20151209161839470)

<2>使用import android.os.Parcelable;中的Parcelable()方法来序列化;

    修改suSchoole.java如下:

public class suShoole 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;
}

public suShoole(String name , int age){
this.age=age;
this.name=name;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
//系统执行到该方法时,会将对象保存并传输给其他组件或者activity
dest.writeString(getName());
dest.writeInt(getAge());
}
}

Android Studio新建myFirstActivity.java


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

public class myFirstActivity extends AppCompatActivity {

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

        findViewById(R.id.secondbtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(myFirstActivity.this , mySecondActivity.class);

                i.putExtra("shoole" , new suShoole("elliott" , 18));
                startActivity(i);
            }
        });

    }
}

Android Studio新建mySecondActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class mySecondActivity extends AppCompatActivity {

            private TextView  myscondTextview = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_second);

        myscondTextview = (TextView) findViewById(R.id.myScondActivty);

        Intent i = getIntent();
        suShoole shoole = (suShoole) i.getParcelableExtra("shoole");
        myscondTextview.setText(String.format(" shoole info(name=%s , age=%d)" ,shoole.getName(), shoole.getAge()));
    }
}



效果图:
Android学习笔记之Activity间传递参数_第3张图片

点击button
Android学习笔记之Activity间传递参数_第4张图片

你可能感兴趣的:(android,intent,getString,putString)