activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到其它Activity"
android:textAllCaps="false"
android:background="@android:color/holo_blue_light"
android:textColor="@android:color/white"
android:textSize="25sp"
android:padding="5dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
MainActivity
package com.etime.myproject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* 为便于测试,请按照以下要求进行:
* 1、MainActivity的mButton.setOnClickListener()中调用testIntent1时
* 在AnotherActivity的mButton.setOnClickListener()中调用testIntent1
*
* 2、MainActivity的mButton.setOnClickListener()中调用testIntent2时
* 在AnotherActivity的mButton.setOnClickListener()中调用testIntent2
*
* 3、请在配置文件AndroidManifest中配置AnotherActivity
* <activity android:name=".AnotherActivity"/>
*/
public class MainActivity extends Activity {
private Button mButton;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
mButton=(Button)findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
testIntent2();
}
});
}
//利用Intent启动另外的Activity
private void testIntent1(){
Intent intent=new Intent(mContext,AnotherActivity.class);
startActivity(intent);
}
//利用Intent启动另外的Activity
private void testIntent2(){
Intent intent=new Intent(mContext,AnotherActivity.class);
intent.putExtra("name","hanmeimei");
intent.putExtra("age",19);
startActivity(intent);
}
}
activity_another
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Another Activity"
android:background="@android:color/holo_blue_light"
android:textColor="@android:color/white"
android:textSize="20sp"
android:padding="4dp"
android:textAllCaps="false"
android:layout_centerInParent="true"/>
</RelativeLayout>
AnotherActivity
package com.etime.myproject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class AnotherActivity extends Activity {
public static final String TAG="TAG";
private Button mButton;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
init();
}
private void init(){
mContext=this;
mButton=(Button)findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//关闭页面
finish();
}
});
testIntent2();
}
private void testIntent1(){
}
private void testIntent2(){
Intent intent=this.getIntent();
String name=intent.getStringExtra("name");
int age=intent.getIntExtra("age",0);
Log.i(TAG,"name="+name);
Log.i(TAG,"age="+age);
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.etime.myproject">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注册Activity -->
<activity android:name=".AnotherActivity"/>
</application>
</manifest>