源码下载↓↓↓
跟着视频一步一步做起来....首先大家都知道在程序中常常都看到需要切换界面,比如点击登录或者一个功能按钮,页面就会变换。往往界面更换的时候,都需要把前一个页面的参数向后一个页面进行传递,。
1.Activity:我自己理解它就是一个界面,里面可以放置一个文本框、按钮什么的,而整个应用程序就是由N个Activity组合而成的。
2.Intent:给我的感觉它就想web开发中的request,提交请求,传递参数。
下面介绍4中传参方式:
1.通过Intent方式传参
2.通过系统剪切板传参、需要对象转化成Base64的字符串进行传递
3.通过Application全局对象传值
4.通过Activity中的静态变量传参
好了,其他基本的东西部说了,自己百度。直接上相关代码;
Main.java 跳转前的页面
package com.example.myintent;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class Main extends Activity {
MyApp myApp;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//加载布局文件
//获取在布局文件中定义的button对象
button = (Button)this.findViewById(R.id.button);
//为button设置点击事件
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/**
* 1.Intent 普通Intent参数传递
*/
//构造意图组件,根据本类和将要跳转的意图class
// Intent intent = new Intent(Main.this,OtherActivtiy.class);
// intent.putExtra("name", "父亲");//在意图中传递参数
// intent.putExtra("age", 40);
// intent.putExtra("address", "四川");
// startActivity(intent);//执行跳转
/**
* 2.Intent 使用系统剪切板传递
*/
//构造意图组件,根据本类和将要跳转的意图class
// Intent intent = new Intent(Main.this,OtherActivtiy.class);
// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// String base64String = "";
// try {
// ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
// MyData myData = new MyData("父亲",41);
// objectOutputStream.writeObject(myData);
// base64String = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
// objectOutputStream.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// clipboardManager.setText(base64String);
// startActivity(intent);//执行跳转
/**
* 3.Intent 使用全局Application传值
*/
//构造意图组件,根据本类和将要跳转的意图class
Intent intent = new Intent(Main.this,OtherActivtiy.class);
myApp = (MyApp) getApplication();
myApp.setName("APP");
myApp.setAge(10);
startActivity(intent);//执行跳转
/**
* 4.使用静态变量传递参数
*/
//构造意图组件,根据本类和将要跳转的意图class
// Intent intent = new Intent();
// intent.setClass(Main.this, OtherActivtiy.class);
// OtherActivtiy.name = "静态变量------父亲";
// OtherActivtiy.age = 41;
// startActivity(intent);//执行跳转
}
});
}
}
main.java 对应的布局 activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myintent.Main"
tools:ignore="MergeRootFrame" >
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button">
</Button>
</FrameLayout>
OtherActivtiy.java
跳转后的页面
package com.example.myintent;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.widget.TextView;
public class OtherActivtiy extends Activity{
MyApp myApp;
public static String name;
public static int age;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);//加载布局文件
textView = (TextView) this.findViewById(R.id.msg);//获取布局中的textview组件
/**
* 1.Intent 普通Intent参数传递
*/
// Intent intent = getIntent();//得到意图Intent
// int age = intent.getIntExtra("age", 0);//获取传递参数,int型许设置默认值
// String name = intent.getStringExtra("name");
// String address = intent.getStringExtra("address");
// //设置参数到到组件中,显示
// textView.setText("name:"+name+"\n age:"+age+"\n address:"+address);
/**
* 2.Intent 使用系统剪切板传递
*/
//设置参数到到组件中,显示
// ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
// String base64String = clipboardManager.getText().toString();
// byte[] byte64 = Base64.decode(base64String, Base64.DEFAULT);
// ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byte64);
// try {
// ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
// MyData myData = (MyData) objectInputStream.readObject();
// objectInputStream.close();
// textView.setText(myData.toString());
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
/**
* 3.Intent 使用全局Application传值
*/
Intent intent = getIntent();//得到意图Intent
myApp = (MyApp) getApplication();
//设置参数到到组件中,显示
textView.setText("name:"+myApp.getName()+"\n age:"+myApp.getAge());
/**
* 4.Intent 使用静态变量传递
*/
// Intent intent = getIntent();//得到意图Intent
// //设置参数到到组件中,显示
// textView.setText("name:"+name+"\n age:"+age);
}
}
OtherActivtiy.java 对应的布局 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">android_Intent</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="button">点击跳转</string>
</resources>
MyApp.java
package com.example.myintent;
import android.app.Application;
public class MyApp extends Application {
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;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
setName("初始化Name");
setAge(999);
}
}
MyData.java
package com.example.myintent;
import java.io.Serializable;
public class MyData implements Serializable{
private String name;
private int age;
public String getName() {
return name;
}
public MyData(String name, int age) {
super();
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "name:"+this.name+"\t age:"+this.age;
}
}
点击下载源码