上一篇博客讲到Intent实现页面跳转,先来回顾一下:
Intent可以理解为信使(直译为意图),由 Intent来协助完成Android各个组件间的通讯
Intent实现页面跳转方法:
1> startActivity(Intent),实例如下:
//文件first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="跳转页面"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
<TextView
android:text="这是第一个页面"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView" />
</LinearLayout>
//文件second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="这是第二个页面"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3" />
</LinearLayout>
//文件first.java
package com.exam.json.test;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class first extends AppCompatActivity {
private Button btn;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
mContext=this;
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(mContext,second.class);
startActivity(intent);
}
});
}
}
//文件second.java
package com.exam.json.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class second extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
2> startActivityForResult(intent,requestCode);
与onActivityResult()和setResult()方法连用,实例如下:
//文件first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="页面跳转"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
<TextView
android:text="显示第二个页面传过来的数据"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView" />
</LinearLayout>
//文件second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="返回"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button2" />
</LinearLayout>
//文件first.java
package com.examq.json.test1;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class first extends AppCompatActivity {
private Button btn;
private Context mContext;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
mContext=this;
btn=(Button)findViewById(R.id.button);
tv=(TextView)findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(mContext,second.class);
//第一个参数是Intent对象,第二个参数是返回值
startActivityForResult(intent,1);
}
});
}
//通过startActivityForResult跳转,接受返回数据方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//requestCode:请求的标识,resultCode:第二个页面返回的标识,data:第二个页面回传的数据
if(requestCode==1&&resultCode==2){
String content=data.getStringExtra("data");
tv.setText(content);
}
}
}
//文件second.java
package com.examq.json.test1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class second extends AppCompatActivity {
private Button btn2;
private String content="你好";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
//第二个页面什么时候给第一个页面回传数据
//回传到第一个页面的实际上是一个Intent对象
btn2=(Button)findViewById(R.id.button2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent data=new Intent();
data.putExtra("data",content);
setResult(2,data);
//结束当前页面
finish();
}
});
}
}
【注意】记得每建立一个Java文件就要在AndroidManifest.xml中声明。