关于在Android中Activity页面跳转的方法
一.无返回结果的页面跳转
1.创建两个类FActivity.java和SActivity.java
2.创建两个layout目录下的factivity.xml和sactivity.xml
3.在AndroidManifest配置文件里注册FActivity.java和SActivity.java,并把原来MainActivity注册信息中的
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
删去,这几句是设置初始启动的Activity页面
4.在FActivity的注册信息中添加
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
5.实现从FActivity页面中的button1点击后跳转到SActivity页面
FActivity.java
public class FActivity extends Activity{ private Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.factivity); btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(FActivity.this,SActivity.class); startActivity(intent); } }); } }
SActivity.java
public class SActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sactivity); } }
factivity.xml
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个页面" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转第二个页面" />
sactivity.xml
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是第二个页面" />
二.有返回结果的页面跳转
1.创建两个类FActivity.java和SActivity.java
2.创建两个layout目录下的factivity.xml和sactivity.xml
3.在AndroidManifest配置文件里注册FActivity.java和SActivity.java,并把原来MainActivity注册信息中的
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
删去,这几句是设置初始启动的Activity页面
4.在FActivity的注册信息中添加
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
5.实现从FActivity页面中的button点击后跳转到SActivity页面,再点击SActivity的button从SActivity传回一个值到FActivity
FActivity.java
public class FActivity extends Activity{ private Button btn2; private TextView tv2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.factivity); btn2 = (Button) findViewById(R.id.button2); tv2 = (TextView) findViewById(R.id.textView2); btn2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(FActivity.this,SActivity.class); startActivityForResult(intent, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(requestCode==1&&resultCode==2) { String content=data.getStringExtra("data"); tv2.setText(content); } } }
SActivity
public class SActivity extends Activity{ private Button btn1; private String content="我是传回来的值"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sactivity); btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent data = new Intent(); data.putExtra("data", content); setResult(2,data); finish(); } }); } }
factivity.xml
<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="传值的的跳转第二个界面" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结果" />
sactivity.xml
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="带值返回" />