一、新建OtherActivity.java
public class OtherActivity extends Activity {
public static String name;
public static int age;
private TextView textview;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textview = (TextView)this.findViewById(R.id.msg);
textview.setText("name:"+name+",age:"+age);
}
}
定义静态变量name,age.
二、在MainActivity.java,跳转页面的时候调用class OtherActivity的静态变量name、age参数传入
public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this,OtherActivity.class);
OtherActivity.age = 23;
OtherActivity.name ="deng";
startActivity(intent);
}
});
}
}
三、附上xml
1)activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="静态变量传参数测试"
/>
2)other.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
3)AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
package="com.example.intent_static"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.example.intent_static.MainActivity"
android:label="@string/app_name" >
android:name=".OtherActivity"
/>